I use curl to get http headers to find http status code and also return response. I get the http headers with the command
curl -I http://localhost
To get the response, I use the command
curl http://localhost
As soon as use the -I flag, I get only the headers and the response is no longer there. Is there a way to get both the http response and the headers/http status code in in one command?
I found this question because I wanted BOTH the response and the content in order to add some error handling for the user.
You can print the HTTP status code to std out and write the contents to another file.
This let's you use logic to decide if the response is worth processing.
This command
will get the comma separated body and status; you can split them to get them out.
You can change the delimiter as you like.
For programmatic usage, I use the following :
It shows following output :
the verbose mode will tell you everything
I was able to get a solution by looking at the curl doc which specifies to use - for the output to get the output to stdout.
To get the response with just the http return code, I could just do
This is a way to retrieve the body "AND" the status code and format it to a proper json or whatever format works for you. Some may argue it's the incorrect use of write format option but this works for me when I need both body and status code in my scripts to check status code and relay back the responses from server.
run the code above and you should get back a json in this format:
with the -w write format option, since stderr is printed first, you can format your output with the var http_code and place the body of the response in a value (body) and follow up the enclosing using var stdout. Then redirect your stderr output to stdout and you'll be able to combine both http_code and response body into a neat output