gpt4 book ai didi

python - 使用 Python 请求和 x-www-form-urlencoded 发送嵌套 json 数据

转载 作者:太空宇宙 更新时间:2023-11-03 16:12:06 26 4
gpt4 key购买 nike

我不知道该怎么做。基本上有一个特定的 API 需要

'Content-Type': 'application/x-www-form-urlencoded;字符集=UTF-8'

但他们希望表单数据是这样的:

stockLevels=[
{
"SKU": "sample string 1",
"LocationId": "de0f1890-0c49-4834-b8cd-8b766fba496a",
"Level": 3
},
{
"SKU": "sample string 1",
"LocationId": "de0f1890-0c49-4834-b8cd-8b766fba496a",
"Level": 3
}
]

我正在做的是这样的:

def change_stock_quantity(self, SKU, qty):
# Note that this will change quantity of just one item
# instead of two like in the example above
payload = {
'stockLevels':[{
'SKU': SKU,
'LocationId': self.ebay1_location_id,
'Level': qty,
}]
}
r = self.session.post(self.session_server + '//api/Stock/SetStockLevel',
data=payload)

print r.request.body
# stockLevels=SKU&stockLevels=LocationId&stockLevels=Level
# this is not correct, it should be something like:
# stockLevels:[{"SKU":"P01", "LocationId":"00000000", "Level":4}]

print r
# <Response [400]> (obivously)

关于我做错了什么有什么想法吗?

最佳答案

挖掘完这个GitHub repo's examples ,他们的方法似乎如下:

  1. 正文确实是x-www-form-urlencoded
  2. 复杂参数是针对 x-www-form-urlencoded 编码的 JSON 字符串

所以看看their PHP example对于 CreateVariationGroup打电话,试试这个:

stockLevels=[
{
"SKU": "sample string 1",
"LocationId": "de0f1890-0c49-4834-b8cd-8b766fba496a",
"Level": 3
},
{
"SKU": "sample string 1",
"LocationId": "de0f1890-0c49-4834-b8cd-8b766fba496a",
"Level": 3
}
]

payload = {'stockLevels' : json.dumps(stockLevels)}

# send payload encoded with x-www-form-urlencoded

关于python - 使用 Python 请求和 x-www-form-urlencoded 发送嵌套 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39191187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com