簡體   English   中英

如何發送無數據的POST請求

[英]How to send POST request with no data

是否可以使用urlliburllib2通過POST請求不發送任何數據? 聽起來很奇怪,但是我使用的API在POST請求中發送了空白數據。

我已經嘗試了以下方法,但是由於沒有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)

我收到404異常,這是API嘗試通過“ GET”請求訪問頁面時返回的內容。 我檢查了所有變量,以確保它們正確設置。

有意見嗎?

您的診斷不正確; 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'}

注意事項:

  • http://httpbin.org/post路由僅接受POST方法; 否則將返回405響應。
  • req.get_method()方法用於確定發送請求時使用哪種方法。 您可以看到,即使values空,它也會使用POST req.get_method()確定基於req.has_data()響應使用的方法,並且當data屬性不是None時, 方法返回True 空字符串在這里被視為具有數據。
  • http://httpbin.org/post響應顯示請求標頭; content-length標頭設置為0,表示POST正文為空。

那么問題來了:您如何確定擁有POST成功所需的一切? 可能是需要Referer標頭,或者您可能誤解了傳入的參數,並且POST正文並不意味着是空的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM