如何从 R POST JSON 到 AEM JCR?
How to POST JSON to AEM JCR from R?
我已经为此苦思冥想了,哦,现在三个星期了,我真的很感激任何 tips/hints/ideas。我知道以下内容不可重现(我认为,但话又说回来,我对 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) 正确的 headers 在我的 post 和 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 的人。
我已经为此苦思冥想了,哦,现在三个星期了,我真的很感激任何 tips/hints/ideas。我知道以下内容不可重现(我认为,但话又说回来,我对 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) 正确的 headers 在我的 post 和 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 的人。