这个小家伙:
产生以下请求(如ngrep所示):
我不想要那个
这是怎么回事?!
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'}))
产生以下请求(如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"}
我不想要那个
Content-Type: application/x-www-form-urlencoded
,我明确地表示我要 ('Content-Type', 'application/json')
。这是怎么回事?!
addheaders
中的标头将被添加,但是如果您使用data
参数,则内容类型已经隐式设置为默认值(x-www-form-urlencoded)。在这种情况下,它优先于addheaders
中的标头。 - mata