2014-07-21 40 views
0

以下curl命令创建CRX一个节点:如何使CQ 5以JSON格式返回响应?

curl -u admin:admin \ 
    -F”jcr:primaryType=nt:unstructured” \ 
    -F”sling:resourceType=foo/bar” ... http://localhost:4502 

然而CQ5返回HTML格式的响应:

<html> 
<head> 
    <title>Content modified ...</title> 
</head> 
    <body> 
    <h1>Content modified ...</h1> 
    ... 
    <p><a href="">Go Back</a></p> 
    <p><a href="...">Modified Resource</a></p> 
    <p><a href="/etc/tags/...-keywords">Parent of Modified Resource</a></p> 
    </body> 
</html> 

有一种方法以工艺的要求,使得响应是JSON格式?

+0

我不确定是否存在,但看起来上面页面中唯一真正有用的部分是状态码(在这种情况下是'201'),如果您没有在浏览器中查看它 - 错误页面是否提供了在JSON中需要的更多有用内容? – anotherdave

+1

@anotherdave为了简化这个问题,我从上面的输出中排除了一些内容,但是响应包含一个'

'部分,其中包含一些可能有用的信息。 – Behrang

回答

3

Apache Sling,在其上基于CQ,基于所请求的Accept报头输出格式:

$ curl -u admin:admin -s -H"Accept:application/json" -Ftest=ok http://localhost:8080/tmp | jq . 
{ 
    "referer": "", 
    "changes": [ 
    { 
     "argument": "/tmp", 
     "type": "created" 
    }, 
    { 
     "argument": "/tmp/test", 
     "type": "modified" 
    } 
    ], 
    "path": "/tmp", 
    "location": "/tmp", 
    "parentLocation": "/", 
    "isCreate": true, 
    "status.code": 201, 
    "status.message": "Created", 
    "title": "Content created /tmp" 
} 

这应该工作相同于CQ。

根据Sling的PostServletOutputContentTypeTest,您还可以使用:http-equiv-accept请求参数而不是Accept标头。