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.
精彩评论