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