urllib2 and json

后端 未结 6 720
北恋
北恋 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条回答
  •  爱一瞬间的悲伤
    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

提交回复
热议问题