I wish to send a header to my Apache server on a Linux box. How can I achieve this via a curl call?
In anaconda envirement through windows the commands should be: GET, for ex:
curl.exe http://127.0.0.1:5000/books
Post or Patch the data for ex:
curl.exe http://127.0.0.1:5000/books/8 -X PATCH -H "Content-Type: application/json" -d '{\"rating\":\"2\"}'
PS: Add backslash for json data to avoid this type of error => Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)
and use curl.exe
instead of curl
only to avoid this problem:
Invoke-WebRequest : Cannot bind parameter 'Headers'. Cannot convert the "Content-Type: application/json" value of type
"System.String" to type "System.Collections.IDictionary".
At line:1 char:48
+ ... 0.1:5000/books/8 -X PATCH -H "Content-Type: application/json" -d '{\" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
I use Postman.
Execute whatever call you want to do. Then, postman provides a handy tool to show the curl code .
Run it in the terminal.
In case you want send your custom headers, you can do it this way:
curl -v -H @{'custom_header'='custom_header_value'} http://localhost:3000/action?result1=gh&result2=ghk
GET (multiple parameters):
curl -X GET "http://localhost:3000/action?result1=gh&result2=ghk"
or
curl --request GET "http://localhost:3000/action?result1=gh&result2=ghk"
or
curl "http://localhost:3000/action?result1=gh&result2=ghk"
or
curl -i -H "Application/json" -H "Content-type: application/json" "http://localhost:3000/action?result1=gh&result2=ghk"
In PHP:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('HeaderName:HeaderValue'));
or you can set multiple:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('HeaderName:HeaderValue', 'HeaderName2:HeaderValue2'));
I've switched from curl to Httpie; the syntax looks like:
http http://myurl HeaderName:value