如何从R将JSON发布到AEM JCR?

问题描述 投票:2回答:1

我一直在为此呐喊,哦,三周了,我真的很感激任何提示/提示/想法。我知道下面的内容是不可重复的(我想,但是我的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中工作却让我感到困扰。有任何想法吗?

r aem jcr sling httr
1个回答
2
投票

因此,在继续对我开了几天之后,我终于想出了如何使这项工作。我发现我必须有1)正确的URL,2)该URL中的正确参数,3)正确格式化(即,未装箱)JSON,4)我的帖子中的正确标题,以及5)正确编码的JSON。

这是最终奏效的......

The JSON I was trying to send:

{"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"}

...Needed to be:

: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消耗你发送的内容是绝对必要的。

The correct JSON needed to be correctly encoded:

aem_json_enc <- URLencode(aem_json_final)

The URL needed to be of this form:

aem_stage_url <- 'http://aem-stage-author.myorg.com:4502/content/myorg/en?:contentType=json&:name=node-name&:operation=import&:replace=true'

And the R code for the actual POST:

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。

© www.soinside.com 2019 - 2024. All rights reserved.