如何发送没有数据的POST请求 [英] How to send POST request with no data

查看:23
本文介绍了如何发送没有数据的POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 urlliburllib2 不发送带有 POST 请求的数据?听起来很奇怪,但我使用的 API 在 POST 请求中发送空白数据.

我尝试了以下方法,但由于没有 POST 数据,它似乎发出了 GET 请求.

url = 'https://site.com/registerclaim?cid=' + int(cid)值 = {}标题 = {'用户代理' : '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'}数据 = urllib.urlencode(值)req = urllib2.Request(url, data, headers)resp = opener.open(req)

我收到 404 异常,这是 API 在尝试通过GET"请求访问页面时返回的异常.我已经检查了所有变量以确保它们设置正确,并且确实如此.

是建议吗?

解决方案

您的诊断不正确;urllib2 发送一个空的 POST 正文:

<预><代码>>>>导入 urllib, urllib2>>>url = 'http://httpbin.org/post?cid=42'>>>标题 = {...用户代理":Mozilla/5.0(Macintosh;Intel Mac OS X 10_9_4)AppleWebKit/537.36(KHTML,如 Gecko)Chrome/37.0.2062.120 Safari/537.36",... 'X-CRFS-Token': 'csrf_token_mocked',...'X-Requested-With':'XMLHttpRequest'... }>>>值 = {}>>>数据 = urllib.urlencode(值)>>>req = urllib2.Request(url, data, headers)>>>req.has_data()真的>>>req.get_method()'邮政'>>>resp = urllib2.urlopen(req)>>>body = resp.read()>>>印刷体{"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"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"}>>>从 pprint 导入 pprint>>>导入json>>>pprint(json.loads(body)){u'args': {u'cid': u'42'},你'数据':你'',你'文件':{},你'形式':{},u'headers': {u'Accept-Encoding': u'identity',u'Connection': u'close',u'内容长度':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'},你'json':无,u'origin': u'81.134.152.4',u'url': u'http://httpbin.org/post?cid=42'}

注意事项:

  • http://httpbin.org/post 路由只接受 POST 方法;否则将返回 405 响应.
  • req.get_method() 方法用于确定发送请求时使用什么方法;你可以看到它会使用 POST 即使 values 为空.req.get_method() 根据 req.has_data() 响应确定使用的方法,那个 方法返回 Truedata 属性不是 None 时.空字符串在此处限定为具有数据.
  • http://httpbin.org/post 响应显示请求标头;content-length 标头设置为 0,表示空的 POST 正文.

那么问题是:您有多确定您拥有 POST 成功所需的一切?可能需要 Referer 标头,或者您误解了传入的参数,并且 POST 正文不是意味着为空.

Is it possible using urllib or urllib2 to send no data with a POST request? Sounds odd, but the API I am using sends blank data in the POST request.

I've tried the following, but it seems to be issuing a GET request because of no POST data.

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. 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:

>>> 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; it'll return a 405 response otherwise.
  • The req.get_method() method is used to determine what method to use when sending the request; you can see that it'll use POST even though values is empty. 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. An empty string qualifies here as having data.
  • The http://httpbin.org/post response shows the request headers; the content-length header was set to 0, indicating an empty POST body.

So the question then is: how certain are you you have everything required for the POST to succeed? 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.

这篇关于如何发送没有数据的POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆