相关疑难解决方法(0)

我可以在 python 中返回一个 String 和 Dict 吗?

以下是我的代码片段。如何修改我的 getUrlAndHeader(),以便我的 requests.post 工作。现在,如果我使用 getUrlAndHeader() ,我会得到 404 响应。有人可以帮忙吗?

import requests
def getUrlAndHeader():
    return "https://someurl.com, headers={'key1': 'val1', 'key2': 'val2'}"

if __name__ == "__main__":
    #200#response = requests.post("https://someurl.com", headers={'key1': 'val1', 'key2': 'val2'}, json=[])
    #404#response = requests.post(getUrlAndHeader(), json=[])
    #print response
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
36
查看次数

python请求带有标头和参数的POST

我有一个发帖请求,试图requests在python中发送。但是我收到无效的403错误。请求可以通过浏览器正常运行。

POST /ajax-load-system HTTP/1.1
Host: xyz.website.com
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-GB,en;q=0.5
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0
Referer: http://xyz.website.com/help-me/ZYc5Yn
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Content-Length: 56
Cookie: csrf_cookie_name=a3f8adecbf11e29c006d9817be96e8d4; ci_session=ba92hlh6o0ns7f20t4bsgjt0uqfdmdtl; _ga=GA1.2.1535910352.1530452604; _gid=GA1.2.1416631165.1530452604; _gat_gtag_UA_21820217_30=1
Connection: close

csrf_test_name=a3f8adecbf11e29c006d9817be96e8d4&vID=9999
Run Code Online (Sandbox Code Playgroud)

我在python中尝试的是:

import requests
import json

url = 'http://xyz.website.com/ajax-load-system'

payload = {
'Host': 'xyz.website.com',
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'en-GB,en;q=0.5',
'Referer': 'http://xyz.website.com/help-me/ZYc5Yn',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest',
'Content-Length': '56', …
Run Code Online (Sandbox Code Playgroud)

python post request python-3.x python-requests

-3
推荐指数
1
解决办法
2万
查看次数

标签 统计

python ×2

post ×1

python-3.x ×1

python-requests ×1

request ×1