Contents
How to get the HTTP status code from curl?
Credits for the generic read with IFS processing go to this answer: https://unix.stackexchange.com/a/430182/45479 . This way we can pipe the curl when it fails, and it also shows the status code. Append a line “http_code:200” at the end, and then grep for the keyword “http_code:” and extract the response code.
How to write a curl script in Bash?
It’s a 200 that represents a successful request. Let’s write a Bash script called http_response.sh that writes the output of the cURL command to a variable and then prints the value of the variable to the shell: When we run the script we get the HTTP response code back:
When to use curl to write the response code?
The cURL –write-out flag can be used to write the HTTP response code to the shell: You can see the HTTP response code at the end of the response, so we are getting near to what we want. But, how can we remove the JSON data from the response?
How to evaluate HTTP response codes from Bash / shell script?
The server in question is a ubuntu 8.04 minimal installation with just enough packages installed to run what it currently needs. There is no hard requirement to do the task in bash, but I’d like it to run in such a minimal environment without installing any more interpreters.
How to get curl to follow a redirect?
Use -v or even -i to get to see the headers only. A redirect page (301, 302 or whatever) MAY contain a body but it also MAY NOT. That is up to the site. Since you get HTTP redirects, you may want to use -L too to make curl follow them.
How to avoid the curl error in PHP?
TRUE to follow any “Location: ” header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many “Location: ” headers that it is sent, unless CURLOPT_MAXREDIRS is set). Or you can simply use https:// in your code to avoid the redirect. Thanks for contributing an answer to Stack Overflow!
Is it possible to use curl on SSL?
We recently migrated to SSL, and the site works great with the exception of one function. The function uses curl in the code below to execute an api located on the same server.