Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get HTTP status code with curl post

Tags:

curl

I'm using curl for GET at the command line on Linux in order to get http response code. The response bodies are printed to standard out, which is fine, but I can't see any solution how to get curl for POST to print the HTTP status code(200, 404, 403 etc). Is this possible?

like image 819
blaCkninJa Avatar asked Mar 28 '19 19:03

blaCkninJa


2 Answers

To get only the http_code you could try:

curl -s -o /dev/null --head -w "%{http_code}" -X POST "https://httpbin.org/post"

you can read/remember it like:

  • -s Silent mode
  • -o Write output to /dev/null
  • --head Get headers only
  • -w "%{http_code}" write out the HTTP Status code

To get all the headers from your request try:

curl -I -X POST "https://httpbin.org/post"

It will return something like:

HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Type: application/json
Date: Thu, 28 Mar 2019 20:12:01 GMT
Server: nginx
Content-Length: 294
Connection: keep-alive
like image 111
nbari Avatar answered Nov 12 '22 10:11

nbari


 curl -X POST --write-out %{http_code} --silent --output /dev/null --data  "{your_json_input}}" -H 'Content-Type:application/json' http://your_url
like image 31
Anurag_BEHS Avatar answered Nov 12 '22 09:11

Anurag_BEHS