2017-10-06 354 views
0

我试图用Python中的POST方法查询服务器。在Python中HTTP POST&302重定向

import requests 

url = "https://www.test.com/service" 
data = {"param": "value"} 

req = requests.post(url, data).content 

当我看着req,它显然没有考虑到我发布的数据。

如果我请与卷曲头:

curl -ik -X POST -d "param=value" https://www.test.com/service 

我得到以下HTTP 302

HTTP/1.1 302 Déplacé Temporairement 
Date: Fri, 06 Oct 2017 16:42:36 GMT 
Server: Apache-Coyote/1.1 
X-Content-Type-Options: nosniff 
X-XSS-Protection: 1; mode=block 
Cache-Control: no-cache, no-store, max-age=0, must-revalidate 
Pragma: no-cache 
Expires: 0 
Location: https://www.test.com/redirect 
Content-Language: fr-FR 
Content-Length: 0 
Via: 1.1 www.test.com 

所以,如果我的理解很好,我重定向到Location这是https://www.test.com/redirect。该地址只能用GET方法查询;如果我尝试使用POST,则会出现HTTP 500错误。

我的理解是,我POST带参数的请求,我被重定向到另一个页面,请求变成GET,查询Location。我不明白我的参数会发生什么:在我的浏览器中,我得到一个答案,考虑到发送的参数,当我用手查询它们时,它们似乎“丢失”了,因为我只是得到一个空的版本https://www.test.com/redirect

我错过了什么?我对请求的理解很浅,所以我确信我错过了一些明显的东西。

回答

0

正确的语法使用requests.postrequests

import requests 

url = "https://www.test.com/service" 
data = {"param":"value"} 

req = requests.post(url, data).content 
+0

我编辑了我的问题,这是一个不幸的错字。我没有使用正确的语法,我确实有这个问题。如果我犯了这个错误,从[我的经验](https://stackoverflow.com/questions/46422307/post-method-in-python-errno-104),它应该会触发'104错误'! – MBR