urllib2 and json

后端 未结 6 604
北恋
北恋 2020-11-29 00:58

can anyone point out a tutorial that shows me how to do a POST request using urllib2 with the data being in JSON format?

相关标签:
6条回答
  • This is what worked for me:

    import json
    import requests
    url = 'http://xxx.com'
    payload = {'param': '1', 'data': '2', 'field': '4'}
    headers = {'content-type': 'application/json'}
    r = requests.post(url, data = json.dumps(payload), headers = headers)
    
    0 讨论(0)
  • 2020-11-29 01:27

    Whatever urllib is using to figure out Content-Length seems to get confused by json, so you have to calculate that yourself.

    import json
    import urllib2
    data = json.dumps([1, 2, 3])
    clen = len(data)
    req = urllib2.Request(url, data, {'Content-Type': 'application/json', 'Content-Length': clen})
    f = urllib2.urlopen(req)
    response = f.read()
    f.close()
    

    Took me for ever to figure this out, so I hope it helps someone else.

    0 讨论(0)
  • 2020-11-29 01:29

    Messa's answer only works if the server isn't bothering to check the content-type header. You'll need to specify a content-type header if you want it to really work. Here's Messa's answer modified to include a content-type header:

    import json
    import urllib2
    data = json.dumps([1, 2, 3])
    req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
    f = urllib2.urlopen(req)
    response = f.read()
    f.close()
    
    0 讨论(0)
  • 2020-11-29 01:38

    To read json response use json.loads(). Here is the sample.

    import json
    import urllib
    import urllib2
    
    post_params = {
        'foo' : bar
    }
    
    params = urllib.urlencode(post_params)
    response = urllib2.urlopen(url, params)
    json_response = json.loads(response.read())
    
    0 讨论(0)
  • 2020-11-29 01:39

    Example - sending some data encoded as JSON as a POST data:

    import json
    import urllib2
    data = json.dumps([1, 2, 3])
    f = urllib2.urlopen(url, data)
    response = f.read()
    f.close()
    
    0 讨论(0)
  • 2020-11-29 01:47

    You certainly want to hack the header to have a proper Ajax Request :

    headers = {'X_REQUESTED_WITH' :'XMLHttpRequest',
               'ACCEPT': 'application/json, text/javascript, */*; q=0.01',}
    request = urllib2.Request(path, data, headers)
    response = urllib2.urlopen(request).read()
    

    And to json.loads the POST on the server-side.

    Edit : By the way, you have to urllib.urlencode(mydata_dict) before sending them. If you don't, the POST won't be what the server expect

    0 讨论(0)
提交回复
热议问题