开发者

Python urllib post with different content type than urlencoded

It 开发者_Go百科is my understanding that, I need to use url encoded content for posting with urllib. Is it possible to use application/json content type when posting with urllib?


Not with urllib, as you'd need to set the Content-type header, and urllib does not provide a way to do so. You can do it however with urllib2 (but also not with urlopen() wich is more meant to "open files with a url" rather than submitting data):

import urllib2
req = urllib2.Request('http://www.example.com/', data="abc", headers={'Content-type': 'text/plain'})
r = urllib2.urlopen(req)

Personally, I prefer httplib2 (3d party) as a http client library.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜