cURL is versatile URL request tool that is vastly used among web developers, everyone should know at least basics. If you have any problems with endpoint provided by backend it is best to provide in bug ticket cURL request that is failing. You don't argue with cURL.
curl 'https://example.com/posts/1'
Note \
at the end of the first line, it is new line brake character.
curl --header 'accept: application/json, text/plain, */*' \
'https://example.com/posts/1'
curl 'https://example.com/posts?userId=1'
curl --header "Content-Type:application/json" \
--data "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":\"1\"}" \
'https://example.com/posts'
The most common mistake in this type of query is forcing cURL
to change request type by -X POST
or --request POST
. cURL
don't need this for POST, it will invoke POST method thanks to --data
parameter.
JSON placed in --data
parameter must be stringified. There are a lot of tools to stringify JSON online, like JSON Stringify Online Tool.
curl -F firstName=john \
-F lastName=doe \
-F cv=@pathToFile.pdf 'http://example.com/submit'
curl --request PUT \
--data "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":\"1\"}" \
--header "Content-Type:application/json" \
'https://example.com/posts/1'
Now -X PUT
or --request PUT
is expected. POST, PUT and PATCH are similar, cURL
must know which request type we would like to perform.
curl --request PATCH \
--header "Content-Type:application/json" \
--data "{\"title\":\"foo\"}" \
'https://example.com/posts/1'
curl --request DELETE \
'https://example.com/posts/1'
Most likely you would like to get response header and response code, then add --include
or short -i
option with your request.
curl 'https://example.com/posts/1' -i
Add --verbose
or -v
.
curl 'https://example.com/posts/1' -v