2013 février 7 Un commentaire
How to get indented JSON with curl output
You need to install simplejson Python module in your environment, or python-simplejson.
curl [options] url | python -msimplejson.tool
An example with github APIv3
curl -s -H "Content-Type: application/json" -X GET \ "https://api.github.com/repos/collective/collective.ckeditor" \ | python -msimplejson.tool
How to display response headers and API errors with the JSON indentation
This command line have two problems :
- You cannot display response HTTP headers because it breaks simplejson parsing
- If you have an error with a request and you don’t know how to debug it.
You need to add two more options to push header response and error message in stderr: -S and -D /dev/stderr. Now you can test your API with all informations in one command:
curl -s -S -D /dev/stderr -u user:password \ -H "Content-Type: application/json" -X GET \ "http://myapi/v1/test/"
From curl manual:
- -s no progress bar; stats report or errors are dislayed
- -S display errors if -s is enable
- -D dump the header in a file; here stderr to make them not interfer with json indentation
- -u user and password for Basic HTTP Authentication
- -H add header in the outgoing request
- -X HTTP Command to launch. If you’re using Basic HTTP Authentication you must use -X GET
If someone have tip to add pygment coloration in json and headers I will be glad to share the ownership of this article.