Behrang Saeedzadeh
Behrang Saeedzadeh

Reputation: 47913

How to make CQ 5 return response in JSON format?

The following curl command creates a node in CRX:

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

However CQ5 returns the response in HTML format:

<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>

Is there a way to craft the request so that the response is in JSON format?

Upvotes: 0

Views: 1060

Answers (1)

Bertrand Delacretaz
Bertrand Delacretaz

Reputation: 6100

Apache Sling, on which CQ is based, selects the output format based on the request's Accept header:

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

That should work the same in CQ.

As per Sling's PostServletOutputContentTypeTest, you can also use an :http-equiv-accept request parameter instead of the Accept header.

Upvotes: 3

Related Questions