delavnog delavnog - 3 months ago 17
Python Question

Not possible to set content-type to application/json using urllib2

This little baby:

import urllib2
import simplejson as json

opener = urllib2.build_opener()
opener.addheaders.append(('Content-Type', 'application/json'))
response = opener.open('http://localhost:8000',json.dumps({'a': 'b'}))


Produces the following request (as seen with ngrep):

sudo ngrep -q -d lo '^POST .* localhost:8000'

T 127.0.0.1:51668 -> 127.0.0.1:8000 [AP]
POST / HTTP/1.1..Accept-Encoding: identity..Content-Length: 10..Host: localhost:8000..Content-Type: application/x-www-form-urlencoded..Connection: close..User-Agent:
Python-urllib/2.7....{"a": "b"}


I do not want that
Content-Type: application/x-www-form-urlencoded
. I am explicitely saying that I want
('Content-Type', 'application/json')


What's going on here?!

Answer

If you want to set custom headers you should use a Request object:

import urllib2
import simplejson as json

opener = urllib2.build_opener()
req = urllib2.Request('http://localhost:8000', data=json.dumps({'a': 'b'}),
      headers={'Content-Type': 'application/json'})
response = opener.open(req)