[英]HTTPError when sending POST with urllib2 with params
我正在嘗試使用此代碼發送帶有一些參數的POST請求:
def _ws_save_manifest(self):
url = 'http://localhost:8000/extras/save_manifest/'
xml_string = 'example'
params = {'xml_string': xml_string}
data = urllib.urlencode(params)
request = urllib2.Request(url, data)
request.add_header('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5')
request.add_header('Content-type', 'application/x-www-form-urlencoded')
response = urllib2.urlopen(request)
#response = urllib2.urlopen(url)
result = json.load(response)
if result['ok']:
return True
return False
但是我收到以下錯誤:
File "/usr/lib/python2.7/urllib2.py", line 528, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: FORBIDDEN
使用GET,我一點都沒有問題,但是在發送標頭時出現了問題。
順便說一句:請求使用的是django,這就是url(與GET完美配合):
url(r'^extras/save_manifest/$', 'extras.views.save_manifest'),
問題是,帶有django的POST請求受跨站點請求偽造保護。 請參閱https://docs.djangoproject.com/en/dev/ref/contrib/csrf/了解更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.