[英]How to send POST request with no data
Is it possible using urllib
or urllib2
to send no data with a POST
request? 是否可以使用
urllib
或urllib2
通过POST
请求不发送任何数据? Sounds odd, but the API I am using sends blank data in the POST
request. 听起来很奇怪,但是我使用的API在
POST
请求中发送了空白数据。
I've tried the following, but it seems to be issuing a GET
request because of no POST
data. 我已经尝试了以下方法,但是由于没有
POST
数据,它似乎正在发出GET
请求。
url = 'https://site.com/registerclaim?cid=' + int(cid)
values = {}
headers = {
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36',
'X-CRFS-Token' : csrfToken,
'X-Requested-With' : 'XMLHttpRequest'
}
data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
resp = opener.open(req)
I am getting a 404 exception, which is what the API returns if trying to access the page via a 'GET' request. 我收到404异常,这是API尝试通过“ GET”请求访问页面时返回的内容。 I've checked all the variables to ensure they are set correctly, and they are.
我检查了所有变量,以确保它们正确设置。
Am advice? 有意见吗?
Your diagnosis is incorrect; 您的诊断不正确;
urllib2
will send an empty POST body in your case: urllib2
在您的情况下将发送一个空的POST正文:
>>> import urllib, urllib2
>>> url = 'http://httpbin.org/post?cid=42'
>>> headers = {
... 'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36',
... 'X-CRFS-Token': 'csrf_token_mocked',
... 'X-Requested-With' : 'XMLHttpRequest'
... }
>>> values = {}
>>> data = urllib.urlencode(values)
>>> req = urllib2.Request(url, data, headers)
>>> req.has_data()
True
>>> req.get_method()
'POST'
>>> resp = urllib2.urlopen(req)
>>> body = resp.read()
>>> print body
{"args": {"cid": "42"}, "data": "", "files": {}, "form": {}, "headers": {"Accept-Encoding": "identity", "Connection": "close", "Content-Length": "0", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36", "X-Crfs-Token": "csrf_token_mocked", "X-Request-Id": "a14f84f5-a355-4b8a-8b34-cb42808b8b09", "X-Requested-With": "XMLHttpRequest"}, "json": null, "origin": "81.134.152.4", "url": "http://httpbin.org/post?cid=42"}
>>> from pprint import pprint
>>> import json
>>> pprint(json.loads(body))
{u'args': {u'cid': u'42'},
u'data': u'',
u'files': {},
u'form': {},
u'headers': {u'Accept-Encoding': u'identity',
u'Connection': u'close',
u'Content-Length': u'0',
u'Content-Type': u'application/x-www-form-urlencoded',
u'Host': u'httpbin.org',
u'User-Agent': u'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36',
u'X-Crfs-Token': u'csrf_token_mocked',
u'X-Request-Id': u'a14f84f5-a355-4b8a-8b34-cb42808b8b09',
u'X-Requested-With': u'XMLHttpRequest'},
u'json': None,
u'origin': u'81.134.152.4',
u'url': u'http://httpbin.org/post?cid=42'}
Some things to note: 注意事项:
req.get_method()
method is used to determine what method to use when sending the request; req.get_method()
方法用于确定发送请求时使用哪种方法。 you can see that it'll use POST
even though values
is empty. values
空,它也会使用POST
。 req.get_method()
determines the method used based on the req.has_data()
response, and that method returns True
when the data
attribute is not None
. req.get_method()
确定基于req.has_data()
响应使用的方法,并且当data
属性不是None
时, 该方法返回True
。 An empty string qualifies here as having data. So the question then is: how certain are you you have everything required for the POST to succeed? 那么问题来了:您如何确定拥有POST成功所需的一切? Perhaps a
Referer
header is required, or perhaps you misunderstood what parameters are passed in and the POST body is not meant to be empty. 可能是需要
Referer
标头,或者您可能误解了传入的参数,并且POST正文并不意味着是空的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.