【问题标题】:format of data for a http POST resquest when using the curl command使用 curl 命令时 http POST 请求的数据格式
【发布时间】:2017-08-04 08:23:30
【问题描述】:

如果我向REST api 发出curl 命令,那么我会得到以下响应。

curl -i http://10.4.0.22:8088/api/clients/TEST_1/8194/1/1

回复:

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 51
Server: Jetty(9.1.z-SNAPSHOT)

{"status":"CONTENT","content":{"id":1,"value":"0"}}

我的理解是这相当于一个 http GET 请求。

我要做的是更新value 字段并将值更改为1。

我认为这需要使用-d 标志的POST 请求。

我的问题是如何知道 curl 命令中的数据应该是什么格式?

我试过了

curl -d "{"status":"CONTENT","content":{"id":1,"value":"1"}}" http://10.4.0.22:8088/api/clients/EST_1/8194/1/1

但我收到了这个回复。

{"status":"METHOD_NOT_ALLOWED"}

我认为我在 te -d 标志之后指定 json 的方式不正确?

【问题讨论】:

    标签: http curl post get


    【解决方案1】:

    响应 METHOD_NOT_ALLOWED 表示服务器不允许 POST 方法。

    您可以添加 -v 以获得详细的响应输出。有时,服务器会在消息中回复它允许的 HTTP Methods(或 Verbs)。

    我在我的应用程序端点上尝试了您的参数并且它有效。见下文

    curl -d "{"status":"CONTENT","content":{"id":1,"value":"1"}}" http://sitename.azurewebsites.net/index.php -v
    * Adding handle: conn: 0x2c257d0
    * Adding handle: send: 0
    * Adding handle: recv: 0
    * Curl_addHandleToPipeline: length: 1
    * - Conn 0 (0x2c257d0) send_pipe: 1, recv_pipe: 0
    * About to connect() to sitename.azurewebsites.net port 80 (#0)
    *   Trying 104.211.224.252...
    * Connected to sitename.azurewebsites.net (104.211.224.252) port 80 (#0)
    > POST /index.php HTTP/1.1
    > User-Agent: curl/7.33.0
    > Host: sitename.azurewebsites.net
    > Accept: */*
    > Content-Length: 39
    > Content-Type: application/x-www-form-urlencoded
    >
    * upload completely sent off: 39 out of 39 bytes
    < HTTP/1.1 200 OK
    < Content-Length: 0
    < Content-Type: text/html
    < X-Powered-By: PHP/5.4.45
    < Date: Fri, 04 Aug 2017 14:24:08 GMT
    

    您也可以尝试在请求中指定 Content-Type 标头并将其设置为相应的 Mime 值。

    对于 CURL,您可以使用 -H 并将值设置为 "Content-Type: application/json"

    你看过这个帖子吗:How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?

    【讨论】:

    • 非常感谢您的帮助。
    猜你喜欢
    • 2017-09-06
    • 2013-04-28
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多