106
votes
Can I make cURL fail with an exitCode different than 0 if the HTTP status code is not 200?
Use --fail-with-body. This will cause curl to exit with status code 22 when response code is >=400.
Note though that this flag is brand new (as of 2021/02/15), and was added in version 7.76.0, ...
104
votes
Accepted
Specifying minor TLS version when using curl
Curl has options to control the TLS version used. At the date of the last revision to this answer, if you want to specify that TLS 1.2 is used but not 1.1 or 1.3 etc, you need something like
curl --...
100
votes
Can I make cURL fail with an exitCode different than 0 if the HTTP status code is not 200?
I was able to do it using a combination of flags:
curl --silent --show-error --fail URL
--silent hides the progress and error
--show-error shows the error message hidden by --silent
--fail returns an ...
84
votes
PowerShell equivalent of curl
As of Powershell 5.0, if not before, curl is an alias for Invoke-WebRequest.
PS> Get-Alias -Definition Invoke-WebRequest | Format-Table -AutoSize
CommandType Name Version ...
81
votes
Accepted
Can I make cURL fail with an exitCode different than 0 if the HTTP status code is not 200?
Most of the answers provided so far will not print the HTTP response body in case an HTTP request fails.
If you would like to print the response body as well, even if the exit code is non-zero due to ...
53
votes
Getting curl to output HTTP status code?
Status code ONLY
[0]$ curl -LI http://www.example.org -o /dev/null -w '%{http_code}\n' -s
[0]$ 200
All credit to this GIST
51
votes
How do you display POST data with cURL?
Or you could test with https://httpbin.org/
$ curl https://httpbin.org/post -d "firstname=john&lastname=doe"
{
"args": {},
"data": "",
"files": {},
"form": {
"firstname": "john",
...
50
votes
Accepted
How to POST file contents using cURL?
According to the last section of -d in man curl:
If you start the data with the letter @, the rest should be a file name to read the data from, or - if you want curl to read the data from stdin. ...
47
votes
How do I make a POST request using curl?
If you are lazy, you can get google-chrome or firefox to do all the work for you.
Right-click the form you want to submit and select Inspect (or Inspect Element for Firefox). This will open the ...
44
votes
Accepted
'http" not supported or disabled in libcurl
Ensure you are supplying curl with double quotes ("), not single quotes (')
Curl in Windows is know to not like single quotes as per (for example) this thread:
D:\nr\platform_installer_win32>...
39
votes
How to POST file contents using cURL?
As mentioned in this related question if you want the file uploaded without any changes (curl defaults to stripping carriage-return/line-feed characters) then you may want to use the --data-binary ...
35
votes
Accepted
How to curl case-insensitive
It's not curl that decides case-insensitivity at all – link "checking" is the server's decision.
(In other words, HTTP clients do not have the opportunity to see the list of all possible ...
31
votes
Accepted
curl http_code of 000
The response 000 indicates that cURL failed to execute for some reason. In such a case, you should test for cURL exit code rather than making assumptions. See the "Exit Codes" section of the curl ...
30
votes
How do you display POST data with cURL?
Would like to add netcat alternative
#!/bin/bash
nc -l 8080 &
curl "http://localhost:8080" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
--...
25
votes
How to make a Curl POST call in Windows?
Another option is to mask doublequotes with backslash like this:
curl -i -X POST -H "Content-Type:application/json" -d "{\"firstName\": \"Frodo\", \"lastName\" : \"Baggins\" }" http://localhost:8080/...
25
votes
How to POST file contents using cURL?
To be explicitly clear, the accepted answer suggests:
curl -d "data=@path/to/my-file.txt" http://example.com/
The manual reference is here.
Also see this SE answer and this one also for ...
24
votes
How do you display POST data with cURL?
--trace - works with curl 7.58 and shows you the POST body:
$ curl --trace - -d 'foo=bar' http://example.org
== Info: Rebuilt URL to: http://example.org/
== Info: Trying 2606:2800:220:1:248:1893:...
24
votes
Accepted
How to send POST with body, headers, and HTTP params using cURL?
Not enough reputation to comment so leave this as an answer hoping it help.
curl -L -v --post301 --post302 -i -X PUT -T "${aclfile}" \
-H "Date: ${dateValue}" \
-H "Content-Type: ${contentType}" ...
23
votes
Windows 10 WSL cURL can't access localhost with error (Failed to connect to localhost port 80: Connection refused)
Updated 2023-10-31
Microsoft is fixing this! The following issues are being resolved with a new feature that's available on preview but appears might roll out to regular update paths in the near ...
21
votes
Getting curl to output HTTP status code?
This is a painful curl --fail limitation. From man curl :
-f, --fail
(HTTP) Fail silently (no output at all) on server errors
But there is no way to get both the non-zero return code AND ...
17
votes
How to curl case-insensitive
Case sensitivity for the part after the domain names is up to the server.
To fully emulate case-insensitivity of the server, curl would have to try up to all possible combinations (…LiNUx…, …linUX…) ...
16
votes
Accepted
Get response body and show HTTP code by curl
It looks like the content of the response is a single line.
You could use two read calls to read two lines:
curl -s -w "\n%{http_code}" 'https://swapi.dev/api/people/1?format=json' | {
...
15
votes
Getting curl to output HTTP status code?
For a POST request, the following worked:
curl -w 'RESP_CODE:%{response_code}' -s -X POST --data '{"asda":"asd"}' http://example.com --header "Content-Type:application/json"|grep -o 'RESP_CODE:[1-4][...
15
votes
Specifying minor TLS version when using curl
Curl's behavior has changed!
For versions prior to 7.54.0, RedGrittyBrick's answer remains correct. For version of curl after 7.54.0 the options --tlsv1.0, --tlsv1.1 and --tlsv1.2 set the minimum ...
14
votes
Accepted
SSL cert does not work with IP address for SAN
You're using the wrong SAN type – only actual domain names are allowed in "DNS" type SANs; for IP addresses there is the "iPAddress" type ([7] OCTET STRING).
For OpenSSL you can use:
...
14
votes
Accepted
How do I send a digest auth request using curl?
You don't have to specify all those values anywhere. The only thing
you do have to is username/password pair. CURL takes care of computing
the client response for you. This is exactly what "...
13
votes
Can I make cURL fail with an exitCode different than 0 if the HTTP status code is not 200?
Yes there is a way to do it but is far from obvious as it involves 3 curl options:
curl -s --fail --show-error https://httpbin.org/status/200 > /dev/null
curl -s --fail --show-error https://...
13
votes
Can I make cURL fail with an exitCode different than 0 if the HTTP status code is not 200?
I ended up with this based on Dennis's answer, a quick one-liner that fails for non-200 status codes while retaining the output (to stderr):
[ $(curl ... -o /dev/stderr -w "%{http_code}") -...
13
votes
Accepted
git on Debian 10 backports throws fatal: unable to access 'https://github.com/user/repo.git': Failed sending HTTP2 data
In other computer with the same system, but with git working normally, I compared the packages not upgraded.
Downgrade libcurl3-gnutls to 7.64.0-4+deb10u2 and the problem is solved.
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
curl × 715linux × 135
wget × 127
bash × 93
command-line × 48
windows × 45
download × 44
macos × 41
http × 40
ssl × 35
networking × 34
ubuntu × 33
php × 33
ftp × 26
openssl × 20
terminal × 19
proxy × 19
tls × 19
shell × 17
https × 17
certificate × 15
windows-10 × 13
pipe × 13
ssh × 12
firefox × 12