【问题标题】:Using "Content-Type:application/json" to post in curl gives HTTP/1.1 400 BAD REQUEST使用“Content-Type:application/json”在 curl 中发布 HTTP/1.1 400 BAD REQUEST
【发布时间】:2012-10-12 04:50:48
【问题描述】:

当我使用以下方式发出发布请求时

curl -i -d "username=rock&password=rock" http://my_VM_IP/api/1.1/json/my_login/

它生成所需的响应,生成这样的令牌(删节):

HTTP/1.1 200 OK
Date: Mon, 22 Oct 2012 08:37:39 GMT
Vary: Authorization,Accept-Language,Cookie,Accept-Encoding
Content-Type: text/plain
Transfer-Encoding: chunked
OK{"success": {"my_token": "required_token"}}

但是当我尝试相同的方法时,包括标题:

curl -i -H "Content-Type:application/json" -d "username=rock&password=rock" http://my_VM_IP/api/1.1/json/my_login/ 

它给了我以下错误:

HTTP/1.1 400 BAD REQUEST
Date: Mon, 22 Oct 2012 11:12:04 GMT
Vary: Authorization,Accept-Language,Cookie,Accept-Encoding
***Content-Type: text/plain***
Content-Language: en-us
Connection: close
Transfer-Encoding: chunked
Bad Request

我不明白为什么会这样。还有 为什么 content-Type 显示 text/plain,我还尝试查看其他一些问题,例如 Why Setting POST Content-type:"Application/Json" causes a "Bad Request" on REST WebService? 。它也解决了我遇到的同样的问题。按照答案,我尝试将各种格式的数据作为

{"username":"rock", "password":"rock"} 

但没有成功。提前致谢。

【问题讨论】:

    标签: django api curl http-post django-piston


    【解决方案1】:

    通过使用-H "Content-Type:application/json",您正在为您的请求设置Content-Type 标头。 响应仍将返回您的视图告诉它返回的任何内容。

    要返回 Content-Type application/json 的响应,请使用以下内容:

    import json
    from django.http import HttpResponse
    
    def json_response(return_vars):
        'JSON-encodes return_vars returns it in an HttpResponse with a JSON mimetype'
        return HttpResponse(json.dumps(return_vars), content_type = "application/json")
    
    #Usage: return json_response({'admin_token': admin_api_token.token})
    

    【讨论】:

    • 如果你能详细说明一下,我会很高兴的。目前我只是按如下方式返回令牌: admin_api_token = admin_models.ApiToken.objects.create(admin=admin) [next line] return json_success_response({'admin_token': admin_api_token.token})例外都处理好了。
    • 您需要做的就是 1) 对您的数据进行 JSON 编码,以及 2) 将 content_type = "application/json" 添加到 HttpResponse 构造函数中。在您的情况下,HttpResponsejson_success_response 返回。
    • 添加 content_type 无效.. 添加 Content-Type 作为 return HttpResponse(json.dumps(response),Content-Type = "application/json") 给出 SyntaxError: keyword can't be an表达
    • content_type = "application/json" 没有理由不工作,它直接来自the docs。另一个用法示例是here
    • 特定于您的代码,我会尝试response = json_success_response({'admin_token': admin_api_token.token}); response['Content-Type'] = 'application/json'); return response
    【解决方案2】:

    当您设置 curl 命令的-H 参数时,您指定请求的内容类型。您在响应中看到的响应内容类型是在服务器上设置的。在 WSGI 应用程序中,您需要手动指定 'content-type''content-length'。一些框架提供了返回 JSON 响应的实用方法(例如,Flask 中的jsonify 方法)。

    【讨论】:

      【解决方案3】:

      您很接近,但您需要通过curl 将其以 JSON 格式发送:

      curl -i -H "Content-Type:application/json" -d '{"username":"rock", "password":"rock"}'
      

      "password","admin" 应该是"password":"admin"


      如果这不起作用,请尝试:

      curl --dump-header - -H "Accept:application/json" -H "Content-Type:application/json" -X POST --data '{"username": "admin", "password": "admin"}' http://my_VM_IP/api/1.1/json/my_login/
      

      【讨论】:

      • 非常感谢您的回复。那是一个错字,我已经更正了。如果你能告诉我为什么它仍然显示这个 Content-Type: text/plain
      • @TheRecruit 查看其他答案,您可能没有在回复中正确设置content-type
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 2020-10-26
      • 2014-04-07
      • 1970-01-01
      • 2016-05-06
      • 2012-01-12
      • 1970-01-01
      相关资源
      最近更新 更多