In our lot of posts here in this website like “Send data using http POST from client and receive response from server using netcat over specific port” where we shows how you can post XML data / payload to server using cURL or “How to fix errors related to REST Http POST & JSON for websites developed using openclassifieds / yclas” where we showed how you can post JSON data to server.. so just to summaries in this post we will show how you can do HTTP Post and GET will JSON and XML data.
In this example: we assume our sever URL is http://192.168.0.106:8001/myAPI/ where we are tring to GET / POST the data.
HTTP GET For JSON Data
curl -v -k -X GET -H "\"Accept: application/json\"" -H "\"Content-Type:application/json\"" http://192.168.0.106:8001/myAPI/
HTTP POST For JSON Data
curl -v -k -X POST -H "\"Accept: application/json\"" -H "\"Content-Type:application/json\"" -d '{\"username\":\"MyName\",\"email\":\"MyEmail\",\"age\":\"MyAge\"}' http://192.168.0.106:8001/myAPI/
Note here, We are using -X with argument GET and POST for getting and posting the data respectively. The Header is set to “Accept: application/json” and “-H “Content-Type:application/json” since we are working with JSON payload. Also notice the single quote after -d i.e. ‘{}’ instead of double quote for payload, but sometimes double quote also works.
HTTP GET For XML Data
curl -v -k -X GET -H "\"Accept: application/xml\"" -H "\"Content-Type:text/xml; charset=UTF-8\"" http://192.168.0.106:8001/myAPI/
The GET command remains almost same for JSON and XML with only change in header. ( i.e. -H option )
HTTP POST For XML Data
curl -v -k -X POST -H "\"Accept: application/xml\"" -H "\"Content-Type:text/xml; charset=UTF-8\"" -d '<xml version="1.0" encoding="UTF-8"?>
<username>Myname</username>
<email>MyEmail</email>
<age>Mycity</age>' http://192.168.0.106:8001/myAPI/
The Header is set to “Accept: application/xml” and “-H “Content-Type:application/xml” since we are working with XML payload.