- Official Docs
- In Postman we can click on Code Snippet to show the cURL command (and other command too).
Note that, -X = --request
curl -X [method] [options] [URL]
- The most important options/flags
-B: Use ASCII for text and transfer.-C: Resume an interrupted transfer.-d: Data for the HTTP POST or PUT commands.-E: Use a client certificate file and optional password.-F: Update a HTTP form request from a file.-H: Pass a custom header to the server.-K: Use a file for the configuration.-m: Set a maximum time for the transfer.-N: Disable buffering.-o: Write the output to a file.-s: Run in silent mode.-u: Add a user name and password for the server.-v: Verbose mode, for more details.-X: Specifies the HTTP command to use.-4: Use Ipv4 addresses.-6: Use Ipv6 addresses.-#: Display a progress bar. This is useful for large transfers.
Info
We need to install extensions php-curl
to use it. Checkthe Official Documentation to learn more
- Init cURL
// create & initialize a curl session$curl = curl_init();
/*** Some code**/
// close curl resource to free up system resources// (deletes the variable made by curl_init)curl_close($curl);
- GET method
curl_setopt($curl, CURLOPT_URL, 'https://api.github.com/events');# request headercurl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'User-Agent: cURL' ));$raw = curl_exec($curl);
- POST method
curl_setopt($curl, CURLOPT_URL, 'https://httpbin.org/post');curl_setopt($curl, CURLOPT_POST, 1);# request headercurl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', ));curl_setopt($curl, CURLOPT_POSTFIELDS, '{"key": "value"}');$raw = curl_exec($curl);
- Body response
// Raw text...$raw = curl_exec($curl);
# JSONjson_decode($raw, true);
- Status code ???
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); // $statusCode = 200
curl -X GET '<http://abc:3000/xyz/enpoint?paramOne=1¶mTwo=2>' \ --header 'clientEmail: abc@xyz.com' \ --header 'privateKey: XXXX' }'
The JSON data must be in the form of '{with the "double quotes" inside}'
. This "{single 'quote' inside}"
will not work!
curl -X POST '<http://abc:3000/xyz/enpoint?paramOne=1¶mTwo=2>' \ --header 'clientEmail: abc@xyz.com' \ --header 'privateKey: LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1JSUV2Z0lCQU' \ --header 'Content-Type: application/json' \ --data-raw '{ "dataMain": { "keyOne": "valueOne", "keyTwo": 2 } }'
or with JSON
file
curl -X POST \ --header "Content-Type: application/json" \ --data @.folder/file.json \ <http://localhost:8080/ui/webapp/conf>
or form data
We have a form that can upload file
<form method="POST" enctype='multipart/form-data' action="upload.cgi"> <input type=file name=upload> <input type=submit name=press value="OK"></form>
curl --form upload=@/home/user/Pictures/wallpaper.jpg --form press=OK [URL]