如何使用curlpost获取HTTP状态代码

如何使用curlpost获取HTTP状态代码,curl,Curl,我在Linux上的命令行中使用curl for GET来获取http响应代码。响应主体被打印到标准输出,这很好,但我看不到任何解决方案如何让curl for POST打印HTTP状态代码(200404403等)。这可能吗?要仅获取http\u代码,您可以尝试: curl -s -o /dev/null --head -w "%{http_code}" -X POST "https://httpbin.org/post" 你可以像这样阅读/记忆它: -s静默模式 -o将输出写入/dev/nul

我在Linux上的命令行中使用curl for GET来获取http响应代码。响应主体被打印到标准输出,这很好,但我看不到任何解决方案如何让curl for POST打印HTTP状态代码(200404403等)。这可能吗?

要仅获取http\u代码,您可以尝试:

curl -s -o /dev/null --head -w "%{http_code}" -X POST "https://httpbin.org/post"
你可以像这样阅读/记忆它:

  • -s
    静默模式
  • -o
    将输出写入/dev/null
  • --head
    获取标题 只有
  • -w“%{http\u code}”
    写出http状态代码
要从请求中获取所有标题,请尝试:

curl -I -X POST "https://httpbin.org/post"
它将返回如下内容:

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

如何在windows命令提示符下使用此命令我尝试了命令
curl-i-s-w%{http_code}server:8080/page/
,它抛出
curl:no-URL指定我猜您需要在目标URL中指定协议,如下所示:curl-I-s-w%{http_code}
 curl -X POST --write-out %{http_code} --silent --output /dev/null --data  "{your_json_input}}" -H 'Content-Type:application/json' http://your_url