curl / JSON / Linux / PHP · June 21, 2015 2

Sending JSON or XML as Post using Curl using linux/bash

You can post json data to server using curl in following way

txt='{"id":"124", "title":"test"}'

 curl -i \
        -H "Accept: application/json" \
        -H "Content-Type:application/json" \
        -X POST -d "$txt" "http://username:password@example.com"

Options:

-i, –include

(HTTP) Include the HTTP-header in the output. The HTTP-header includes things like server-name, date of the document, HTTP-version and more

-H, –header

(HTTP) Extra header to include in the request when sending HTTP to a server. You may specify any number of extra headers. Note that if you should add a custom header that has the same name as one of the internal ones curl would use, your externally set header will be used instead of the internal one. This allows you to make even trickier stuff than curl would normally do. You should not replace internally set headers without knowing perfectly well what you’re doing. Remove an internal header by giving a replacement without content on the right side of the colon, as in: -H “Host:”. If you send the custom header with no-value then its header must be terminated with a semicolon, such as -H “X-Custom-Header;” to send “X-Custom-Header:”.

-X, –request

(HTTP) Specifies a custom request method to use when communicating with the HTTP server. The specified request method will be used instead of the method otherwise used (which defaults to GET). Read the HTTP 1.1 specification for details and explanations. Common additional HTTP requests include PUT and DELETE, but related technologies like WebDAV offers PROPFIND, COPY, MOVE and more.

-d, –data

(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded. Compare to -F, –form.

for more details look at curl man page
http://curl.haxx.se/docs/manpage.html

To receive post contents on webserver using PHP follow the link below
https://lempjs.com/2015/06/19/read-json-request-in-php/

Sending XML

curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource

Post Simple Data

curl --data "param1=value1&param2=value2" http://hostname/resource

Restful HTTP POST

curl -X POST -d @filename http://hostname/resource

Uploading File using Curl

curl --form "fileupload=@filename.txt" http://hostname/resource