开发者

Python urllib post with different content type than urlencoded

开发者 https://www.devze.com 2023-04-01 19:17 出处:网络
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 y

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

精彩评论

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

关注公众号