Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.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编写requests.post_Python_Python Requests - Fatal编程技术网

用Python编写requests.post

用Python编写requests.post,python,python-requests,Python,Python Requests,下面我用一个Python脚本连接到一个受登录保护的API import requests url = 'https://api.json' header = {'Content-Type': 'application/x-www-form-urlencoded'} login = ('kjji@snm.com', 'xxxxx') mnem = 'inputRequests':'{'inputRequests':'[{'function':'GDSP','identifier':'ibm','

下面我用一个Python脚本连接到一个受登录保护的API

import requests

url = 'https://api.json'
header = {'Content-Type': 'application/x-www-form-urlencoded'}
login = ('kjji@snm.com', 'xxxxx')
mnem = 'inputRequests':'{'inputRequests':'[{'function':'GDSP','identifier':'ibm','mnemonic':'IQ_TOTAL_REV'}]}}

    r = requests.post(url, auth=login, data=mnem, headers=header)

    print(r.json())
连接已建立,但由于数据请求的格式,API出现错误。原始格式如下。我无法在上面的mnem中输入此信息:

inputRequests={inputRequests:
 [
  {function:"xxx",identifier:"xxx",mnemonic:"xxx"},
   ]
}
给出的错误是

C:\Users\xxx\Desktop>pie.py
  File "C:\Users\xxx\Desktop\pie.py", line 6
    mnem={'inputRequests':'{'inputRequests':'[{'function':'xxx','identifier':'xx','mnemonic':'xxx'}]}}
                                         ^
SyntaxError: invalid syntax

我不确定如何从这里开始。在requests文档中,我找不到任何指向如何在数据字段中插入几个变量的内容。

Python中的
requests
模块接收原始Python
dict
作为post请求中的JSON数据,而不是字符串。因此,您可以尝试这样定义
mnem

mnem = {
    'inputRequests':[
        {'function':'GDSP',
         'identifier':'ibm',
         'mnemonic':'IQ_TOTAL_REV'
        }
   ]}

数据
参数应为字典

因此,要传递这三个参数,请尝试使用:

mnem = {'function':'GDSP','identifier':'ibm','mnemonic':'IQ_TOTAL_REV'}

抱歉,这是最近的一次尝试。我在“”中包含了所有内容,如下所示,但这仍然不起作用:mnem={'inputRequests':{'inputRequests':[{'function':'GDSP','identifier':'ibm','mnemonic':'IQ_TOTAL_REV'}}请包括您收到的错误消息。这会触发API错误:此错误意味着助记符中有错误,因为我缺少“输入请求”部分\Users\xxx\Desktop>pie.py{'Errors':'error Processing the request'}这会触发API的错误:此错误意味着助记符中有错误。我应该提到的是,我是通过邮递员以相同的请求连接到api的,我得到了一个结果,因此上面的输入请求是有效的\用户\xxx\Desktop>pie.py{'Errors':'Error Processing the Request'}可能是这样的:
mnem={'inputRequests':{'inputRequests':[{'function':'GDSP','identifier':'ibm','mnemonic':'IQ_TOTAL_REV'}}
与上述错误相同{'Errors':'Error Processing the Request'此错误是从远程服务器收到的,您的请求一定有问题。请检查请求正文是否正确或是否有任何缺少的参数。
post
请求已成功发送到API。我同意。使用Postman时,API会正确响应下面的正文。没有错误缺少参数。inputRequests={inputRequests:[{函数:“GDSP”,标识符:“ibm”,助记符:“IQ_TOTAL_REV”},]}