Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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
API不接受来自Python的JSON数据_Python_Json_Python Requests - Fatal编程技术网

API不接受来自Python的JSON数据

API不接受来自Python的JSON数据,python,json,python-requests,Python,Json,Python Requests,我不熟悉Python,也不熟悉JSON。我试图从我的数据库中获取字符串数组,并将它们提供给API。我不知道为什么我会得到丢失的数据错误。你们能看一下吗 ########################################### rpt_cursor = rpt_conn.cursor() sql="""SELECT `ContactID` AS 'ContactId' FROM `BWG_reports`.`bounce_log_dummy`;""" rpt_cursor.exe

我不熟悉Python,也不熟悉JSON。我试图从我的数据库中获取字符串数组,并将它们提供给API。我不知道为什么我会得到丢失的数据错误。你们能看一下吗

###########################################

rpt_cursor = rpt_conn.cursor()
sql="""SELECT `ContactID` AS 'ContactId' FROM 
`BWG_reports`.`bounce_log_dummy`;"""
rpt_cursor.execute(sql)

row_headers=[x[0] for x in rpt_cursor.description] #this will extract row headers
row_values= rpt_cursor.fetchall()
json_data=[]
for result in row_values:
    json_data.append(dict(zip(row_headers,result)))
results_to_load = json.dumps(json_data)
print(results_to_load) # Prints: [{"ContactId": 9}, {"ContactId": 274556}]


headers = {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
}

targetlist = '302'
# This is for their PUT to "add multiple contacts to lists".
api_request_url = 'https://api2.xyz.com/api/list/' + str(targetlist) 
+'/contactid/Api_Key/' + bwg_apikey

print(api_request_url) #Prints https://api2.xyz.com/api/list/302/contactid/Api_Key/#####
response = requests.put(api_request_url, headers=headers, data=results_to_load)

print(response) #Prints <Response [200]>
print(response.content) #Prints b'{"status":"error","Message":"ContactId is Required."}'

rpt_conn.commit()
rpt_cursor.close()

###########################################################
为清晰起见,请编辑:

我正在通过这个[{ContactId:9},{ContactId:274556}] 我得到了这个响应体b'{状态:错误,消息:ContactId是必需的。}'

API文档将此作为请求正文的起始项。 [ { 联系人ID:字符串 } ]

当我手动将这些数据放在那个里测试时,我得到了我想要的。 [ { 联系人号码:9 }, { 联系电话:274556 } ]

也许json.dumps与json.load之间有问题?我不是在创建一个dict,而是一个看起来像dict的字符串吗

编辑我想出来了!:

这是愚蠢的

我需要先将results_to_load=[]定义为dict,然后再将其加载到results_to_load=json.dumpsjson_数据中


感谢您提供的所有答案和帮助。

我建议您查看API文档以了解具体情况,但从表面上看,API需要一个名为ContactID的字段,它是一个数组,而不是一个对象数组,其中每个对象都有一个键作为ContactID

而不是

// not correct
[{contactId:9}, {contactId:229}]
调整这一点可能会有所帮助:

res = {}
contacts = []
for result in row_values:
    contacts.append(result)
res[contactId] = contacts
...
...

response = requests.put(api_request_url, headers=headers, data=res)
我想出来了

这是愚蠢的

我需要将results_to_load=[]定义为一个空dict,然后将其加载到results_to_load=json.dumpsjson_数据


感谢您提供的所有答案和帮助。

如果您不清楚地描述错误,就根本无法为您提供帮助。我同意@LTCLIP。这太难读了。他们是在寻找ContactId列表,还是只是一个?你给了它一个清单。我建议你检查一下他们的api。@Ywapom他的json_数据是一个DICT列表,这似乎是合理的。将它初始化为{}而不替换该追加只会给出一个AttributeError,并且不清楚他应该用建议的dict中每个子ct的键来替换该追加,很抱歉造成混淆。我有这样的数据。。。[{ContactId:9},{ContactId:274556}]我得到的响应如下所示。。。b'{状态:错误,消息:ContactId是必需的。}此res['ContactId']=contacts为我提供了{'ContactId':[9,,274556,]},但我仍然只获得ContactId是必需的错误。请检查API文档中预期的请求正文,并将其发布在此处。这可能会有帮助。预期的请求正文为[{ContactId:string}]。我已将其添加到编辑中。
res = {}
contacts = []
for result in row_values:
    contacts.append(result)
res[contactId] = contacts
...
...

response = requests.put(api_request_url, headers=headers, data=res)