curl response json code example
Example 1: curl post json
curl -X POST -H "Content-Type: application/json" \
-d '{"username":"abc","password":"abc"}' \
https://api.example.com/v2/login
Example 2: curl post json
curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/data
Example 3: curl in api
curl is a command line client to send request and
get respond from command line, so it's basically
it's just way to send a request and response. Where you
send it we call it client. But with postman we can do more
than just sending request end respond.
This tool is preferred for automation,
since it is designed to work without user interaction.
curl can transfer multiple file at once.
Also A lot of API documentation provide curl
command for example when we run the request and
Postman can easily import curl command without you
manually typing the whole part of the request.
Example 4: curl post request
$data = [
'api_key' => '123456789',
'name' => 'Emmanuel',
'email' => '[email protected]'
];
$url = 'https://mysite.com/api/subscribe';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$response = curl_exec($ch);
var_export($response);