简体   繁体   English

如何发送无数据的POST请求

[英]How to send POST request with no data

Is it possible using urllib or urllib2 to send no data with a POST request? 是否可以使用urlliburllib2通过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: 注意事项:

  • the http://httpbin.org/post route only accepts POST methods; http://httpbin.org/post路由仅接受POST方法; it'll return a 405 response otherwise. 否则将返回405响应。
  • The 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. 空字符串在这里被视为具有数据。
  • The http://httpbin.org/post response shows the request headers; http://httpbin.org/post响应显示请求标头; the content-length header was set to 0, indicating an empty POST body. content-length标头设置为0,表示POST正文为空。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM