我已经为此绞尽脑汁,哦,三个星期了,我真的很感激任何提示/提示/想法。我知道以下内容不可重现(我认为,但话又说回来,我对 AEM JCR 的了解是有限的),但希望有人会看到我做错的明显事情。好的,我只是尝试从 R 在 AEM 中创建一个基本的顶级节点。我使用 httr,并且我将包含 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
我怀疑我的 URL 中缺少参数,或者我的 JSON 格式错误。我已经让它在 Postman 中工作,但让它在 R 中工作却阻碍了我。有什么想法吗?
最佳答案
因此,在继续为此苦苦思索了几天后,我终于弄清楚了如何实现这项工作。我发现我必须拥有 1) 正确的 URL,2) 该 URL 中的正确参数,3) 格式正确(即未装箱)的 JSON,4) 帖子中的正确 header ,以及 5) 正确编码的 JSON。
这就是最终的效果......
我尝试发送的 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 消耗您发送的内容绝对必要。
需要正确编码正确的 JSON:
aem_json_enc <- URLencode(aem_json_final)
URL 需要采用以下形式:
aem_stage_url <- 'http://aem-stage-author.myorg.com:4502/content/myorg/en?:contentType=json&:name=node-name&:operation=import&:replace=true'
以及实际 POST 的 R 代码:
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 的人。
关于r - 如何从 R 将 JSON 发布到 AEM JCR?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48073247/