我已经在这上面打了三个星期哦,我现在非常感谢任何提示/提示/想法。我知道以下内容不可重复(我想,但是我的AEM JCR知识有限),但希望有人会看到一些明显我做错的事情。好吧,我只是试图在R中使用httr在AEM中创建一个基本的顶级节点。我将包含JSON和R代码:
JSON:
{"content":{"jcr:content":{"cq:designPath":["/etc/designs/myorg"],"cq:template":["/apps/myorg/templates/mynode"],"sling:resourceType":["myorg/components/pages/mynode"],"hideInNav":["true"],"jcr:primaryType":["cq:PageContent"],"jcr:title":["Node Name"]}}}
R代码:
aem_stage_url <- "http://aem-stage-xxxx.mydomain.com:4502/content/myorganization/en?:contentType=json&:nameHint=mynode&:operation=import"
safe_POST <- purrr::safely(httr::POST)
aem_res <- safe_POST(aem_stage_url,
add_headers("Content-Type" = "application/x-www-form-urlencoded"),
authenticate("user" = "myuser", "password" = "mypassword", type = "basic"),
body = json_str,
encode = "form",
verbose(data_out = TRUE, info = TRUE)
)
来自httr的详细输出:
* Connected to aem-stage-xxxx.myorg.com (35.167.72.242) port 4502 (#18)
* Server auth using Basic with user 'myuser'
-> POST /content/myorg/en?:contentType=json&:nameHint=mynode&:operation=import HTTP/1.1
-> Host: aem-stage-xxxx.myorg.com:4502
-> Authorization: Basic KEY==
-> User-Agent: libcurl/7.47.0 r-curl/0.9.3 httr/1.3.1
-> Accept-Encoding: gzip, deflate
-> Cookie: cq-authoring-mode=TOUCH
-> Accept: application/json, text/xml, application/xml, */*
-> Content-Type: application/x-www-form-urlencoded
-> Content-Length: 281
->
>> {"content":{"jcr:content":{"cq:designPath":["/etc/designs/myorg"],"cq:template":["/apps/myorg/templates/mynode"],"sling:resourceType":["myorg/components/pages/mynode"],"hideInNav":["true"],"jcr:primaryType":["cq:PageContent"],"jcr:title":["Node Name"]}}}
* upload completely sent off: 281 out of 281 bytes
<- HTTP/1.1 412 Precondition Failed
<- Date: Wed, 03 Jan 2018 07:35:44 GMT
<- X-Content-Type-Options: nosniff
<- X-Frame-Options: SAMEORIGIN
<- Content-Type: application/json; charset=UTF-8
<- Content-Length: 217
<-
* Connection #18 to host aem-stage-xxxx.myorg.com left intact
我怀疑我在网址中缺少参数,或者我的JSON格式不正确。我已经让它在Postman工作了,但是让它在R中工作却让我感到困扰。有什么想法吗?
答案 0 :(得分:2)
因此,继续对此持续了几天之后,我终于想出了如何使这项工作。我发现我必须有1)正确的URL,2)该URL中的正确参数,3)正确格式化(即,未装箱)JSON,4)我的帖子中的正确标题,以及5)正确编码的JSON。
这里终于有了什么......
{"jcr:content":{"cq:designPath":"/etc/designs/myorg","cq:template":"/apps/myorg/templates/mynode","sling:resourceType":"myorg/components/pages/mynode","hideInNav":"true","jcr:primaryType":"cq:PageContent","jcr:title":"Node Name"}, "jcr:primaryType": "cq:Page"}
:content={"jcr:content":{"cq:designPath":"/etc/designs/myorg","cq:template":"/apps/myorg/templates/mynode","sling:resourceType":"myorg/components/pages/mynode","hideInNav":"true","jcr:primaryType":"cq:PageContent","jcr:title":"Node Name"}, "jcr:primaryType": "cq:Page"}& =
注意(#1) JSON格式必须取消装箱。因此,jsonlite
这是jsonlite::toJSON(aem_json, auto_unbox = TRUE)
注意(#2)开头的:content=
和结尾的& =
。出于某些原因,这些对于让AEM消耗您正在发送的内容是绝对必要的。
aem_json_enc <- URLencode(aem_json_final)
aem_stage_url <- 'http://aem-stage-author.myorg.com:4502/content/myorg/en?:contentType=json&:name=node-name&:operation=import&:replace=true'
safe_POST <- purrr::safely(httr::POST)
aem_res <- safe_POST(aem_stage_url,
add_headers("Content-Type" = "application/x-www-form-urlencoded",
'Authorization: Basic <mykey>'),
authenticate("user" = "node-listener-aem", "password" = "<my_password>", type = "basic"),
body = aem_json_enc, # the body is the encoded json with the extra stuff on the front and the back
verbose(data_out = TRUE, info = TRUE)
)
注意 Content-Type
必须为application/x-www-form-urlencoded
我希望这个答案可以帮助一些试图使用R的AEM工作的人。