Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python urllib发布请求_Python_Http_Urllib - Fatal编程技术网

使用Python urllib发布请求

使用Python urllib发布请求,python,http,urllib,Python,Http,Urllib,我正在使用python3urllib请求一个api。这是我的密码 headers = {} headers['Content-Type']='application/x-www-form-urlencoded; charset=UTF-8' #headers['X-Csrf-Token']= {'mode: cors'} req = urllib.request.Request(url=URL, headers=headers, method='POST') re

我正在使用python3urllib请求一个api。这是我的密码

headers = {}
    headers['Content-Type']='application/x-www-form-urlencoded; charset=UTF-8'
    #headers['X-Csrf-Token']= {'mode: cors'}
    req = urllib.request.Request(url=URL, headers=headers, method='POST')
    res = urllib.request.urlopen(req)
    print(res.read())
我得到了如下的回应:

urllib.error.HTTPError: HTTP Error 415: Unsupported Media Type
API端点工作正常,我已经用Postman对其进行了测试。这是完成任务的等效javascript代码

return fetch(url, { credentials : 'include', method: 'post'})

因此,我假设我必须找到一种方法将凭据添加到头中。我可以在Python中执行凭证包含并行,还是必须获取特定的cookie并在请求中进行设置。感谢您的帮助。

错误415表示“内容类型”存在问题。 “application/x-www-form-urlencoded”没有任何参数

关于fetch()的“凭据”,您需要能够维护会话(根据需要发送cookie)。 使用请求库的会话更容易


每个API都可能不同。没有url do API,我们就无能为力。