I'm using Sonatype Nexus REST core api to get repositories.
The output format is XML. How can I get the output in JSON format?
I can see in the docs that the return type can be application/json
. But I am completely in blank where to set this.
To receive JSON from a REST API endpoint, you must send an HTTP GET request to the REST API server and provide an Accept: application/json request header. The Accept header tells the REST API server that the API client expects JSON. In this REST API GET example, we make a GET request to the ReqBin echo REST API endpoint.
How do I get JSON from a REST API endpoint? To receive JSON from a REST API endpoint, you must send an HTTP GET request to the REST API server and provide an Accept: application/json request header. The Accept header tells the REST API server that the API client expects JSON.
One of the advantages Sonatype Nexus has is that a large portion of our codebase is Open Source Software. You can checkout nexus-oss from Github, open the project in an IDE and find out how the endpoints are defined, including what arguments and payloads are accepted.
Manfred Moser has created a quick 7 minute video that walks through the automation of Nexus with the REST API. This is one module in our free training series. Become a member of TheNEXUS Community and watch the rest of them.
As an example with curl here is a call to get the list of repositories
curl http://localhost:8081/nexus/service/local/repositories
which will give you xml formatted output. To get the same in JSON format you just edit the HTTP header of the request like so
curl -H "Accept: application/json" http://localhost:8081/nexus/service/local/repositories
Potentially you want to add credentials and specify the content type as well (especially if you are posting JSON load as part of the request). You can also change to a POST..
curl -X GET -u admin:admin123 -H "Accept: application/json" -H "Content-Type: application/json" http://localhost:8081/nexus/service/local/repositories
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With