Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将JSON从R发布到AEM JCR?_R_Aem_Jcr_Sling_Httr - Fatal编程技术网

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

如何将JSON从R发布到AEM JCR?,r,aem,jcr,sling,httr,R,Aem,Jcr,Sling,Httr,我已经为此绞尽脑汁了,哦,三个星期了,如果有任何提示/提示/想法,我将不胜感激。我知道我认为下面的内容是不可复制的,但我对AEM JCR的了解是有限的,但希望有人能看到我明显做错了什么。好的,我只是尝试从R在AEM中创建一个基本的顶级节点。我使用的是httr,我将包括JSON和R代码: JSON: R代码: httr的详细输出: 我怀疑我的URL中缺少一个参数,或者我的JSON格式不正确。我在《邮递员》中用过这个,但在《R》中用起来却让我很为难。有什么想法吗?所以在我继续思考这件事几天后,我终于

我已经为此绞尽脑汁了,哦,三个星期了,如果有任何提示/提示/想法,我将不胜感激。我知道我认为下面的内容是不可复制的,但我对AEM JCR的了解是有限的,但希望有人能看到我明显做错了什么。好的,我只是尝试从R在AEM中创建一个基本的顶级节点。我使用的是httr,我将包括JSON和R代码:

JSON:

R代码:

httr的详细输出:


我怀疑我的URL中缺少一个参数,或者我的JSON格式不正确。我在《邮递员》中用过这个,但在《R》中用起来却让我很为难。有什么想法吗?

所以在我继续思考这件事几天后,我终于想出了如何让它起作用。我发现我必须有1个正确的URL,2个正确的URL参数,3个正确的格式,即未绑定的JSON,4个正确的标题,5个正确编码的JSON

以下是最终奏效的方法

我试图发送的JSON: ……需要: 注1:JSON格式必须取消装箱。所以在jsonlite中,这是jsonlite::toJSONaem_json,auto_unbox=TRUE

注2:内容=开头,和&=结尾。出于某些原因,这些对于让AEM使用您发送的内容是绝对必要的

需要正确编码正确的JSON: URL需要采用以下形式: 以及实际职位的R代码: 请注意,内容类型必须是application/x-www-form-urlencoded


我希望这个答案能帮助一些试图从R使用AEM的人。

我应该提到,在《邮递员》中,有效的方法是选择x-www-form-urlencoded,键为:content,值包含JSON的其余部分。我上面发布的JSON是我认为应该发送的,但我不确定我在R方面没有经验。如果没有结果,您可以尝试在代码中运行基于curl的系统命令。@Dileepa感谢您的建议!
{"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"]}}}
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)
)
    *  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
{"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"}& =
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)
)