簡體   English   中英

HTTP request.post失敗

[英]HTTP requests.post fails

我正在使用python請求庫來獲取和發布http內容。 我使用get函數沒有問題,但是post函數似乎失敗或什么也不做。 根據我對請求庫的了解,POST函數會自動對您發送的數據進行編碼,但是我不確定這是否真的在發生

碼:

data = 'hash='+hash+'&confirm=Continue+as+Free+User'   
r = requests.post(url,data)
html = r.text

通過檢查html的“值”,我可以知道返回響應是沒有POST的url的返回響應。

您沒有利用請求如何為您編碼的優勢。 為此,您需要以這種方式編寫代碼:

data = {'hash': hash, 'confirm': 'Continue as Free User'}
r = requests.post(url, data)
html = r.text

我無法為您測試,但這是自動進行編碼的方式。

post(url, data=None, **kwargs)
Sends a POST request. Returns :class:`Response` object.

:param url: URL for the new :class:`Request` object.
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
import requests

url = "http://computer-database.herokuapp.com/computers"

payload = "name=Hello11111122OKOK&introduced=1986-12-26&discontinued=2100-12-26&company=13"
headers = {
    'accept-language': "en-US,en;q=0.9,kn;q=0.8",
    'accept-encoding': "gzip, deflate",
    'accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    'content-type': "application/x-www-form-urlencoded",
    'cache-control': "no-cache",
    'postman-token': "3e5dabdc-149a-ff4c-a3db-398a7b52f9d5"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)

暫無
暫無

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

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