Using "Content-Type:application/json" to post in curl gives HTTP/1.1 400 BAD REQUEST Using "Content-Type:application/json" to post in curl gives HTTP/1.1 400 BAD REQUEST curl curl

Using "Content-Type:application/json" to post in curl gives HTTP/1.1 400 BAD REQUEST


By using -H "Content-Type:application/json" you're setting the Content-Type header for your request. The response will still return whatever your view tells it to return.

To return a response with Content-Type application/json, use something along these lines:

import jsonfrom django.http import HttpResponsedef 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})


You were close, but you need to send it as a JSON format via curl:

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

("password","admin" should be "password":"admin")


If that's not working, try:

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/


When you set -H parameter of curl command, you specify content type of request. Content type of response, that you see in response, is set on the server. In WSGI application you need to specify 'content-type' and 'content-length' manually. Some of framework provide utility method to return JSON responses (for example, jsonify method in Flask).