如何使用请求发送数组。帖子(Python)?“值错误:太多的值要打开”

时间:2022-09-13 13:20:24

I'm trying to send an array(list) of requests to the WheniWork API using requests.post, and I keep getting one of two errors. When I send the list as a list, I get an unpacking error, and when I send it as a string, I get an error asking me to submit an array. I think it has something to do with how requests handles lists. Here are the examples:

我尝试在使用请求的时候向WheniWork API发送一个数组(list)请求。post,我一直得到两个错误中的一个。当我将列表作为列表发送时,我得到了一个解包错误,当我将它作为一个字符串发送时,我得到一个错误,请求我提交一个数组。我认为它与请求处理列表的方式有关。这是例子:

url='https://api.wheniwork.com/2/batch'
headers={"W-Token": "Ilovemyboss"}
data=[{'url': '/rest/shifts', 'params': {'user_id': 0,'other_stuff':'value'}, 'method':'post',{'url': '/rest/shifts', 'params': {'user_id': 1,'other_stuff':'value'}, 'method':'post'}]
r = requests.post(url, headers=headers,data=data)
print r.text

# ValueError: too many values to unpack

Simply wrapping the value for data in quotes:

简单地用引号括起数据的值:

url='https://api.wheniwork.com/2/batch'
headers={"W-Token": "Ilovemyboss"}
data="[]" #removed the data here to emphasize that the only change is the quotes
r = requests.post(url, headers=headers,data=data)
print r.text

#{"error":"Please include an array of requests to make.","code":5000}

2 个解决方案

#1


39  

You want to pass in JSON encoded data. See the API documentation:

您希望传递JSON编码的数据。查看API文档:

Remember — All post bodies must be JSON encoded data (no form data).

记住——所有post主体都必须是JSON编码的数据(没有表单数据)。

The requests library makes this trivially easy:

请求库使这个非常简单:

headers = {"W-Token": "Ilovemyboss"}
data = [
    {
        'url': '/rest/shifts',
        'params': {'user_id': 0, 'other_stuff': 'value'},
        'method': 'post',
    },
    {
        'url': '/rest/shifts',
        'params': {'user_id': 1,'other_stuff': 'value'},
        'method':'post',
    },
]
requests.post(url, json=data, headers=headers)

By using the json keyword argument the data is encoded to JSON for you, and the Content-Type header is set to application/json.

通过使用json关键字参数,数据被编码为json,而内容类型标头被设置为application/json。

#2


10  

Well, It turns out that all I needed to do was add these headers:

我需要做的就是添加这些标题:

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

and than call requests

和比调用请求

requests.post(url,data=json.dumps(payload), headers=headers)

and now i'm good!

现在我很好!

#1


39  

You want to pass in JSON encoded data. See the API documentation:

您希望传递JSON编码的数据。查看API文档:

Remember — All post bodies must be JSON encoded data (no form data).

记住——所有post主体都必须是JSON编码的数据(没有表单数据)。

The requests library makes this trivially easy:

请求库使这个非常简单:

headers = {"W-Token": "Ilovemyboss"}
data = [
    {
        'url': '/rest/shifts',
        'params': {'user_id': 0, 'other_stuff': 'value'},
        'method': 'post',
    },
    {
        'url': '/rest/shifts',
        'params': {'user_id': 1,'other_stuff': 'value'},
        'method':'post',
    },
]
requests.post(url, json=data, headers=headers)

By using the json keyword argument the data is encoded to JSON for you, and the Content-Type header is set to application/json.

通过使用json关键字参数,数据被编码为json,而内容类型标头被设置为application/json。

#2


10  

Well, It turns out that all I needed to do was add these headers:

我需要做的就是添加这些标题:

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

and than call requests

和比调用请求

requests.post(url,data=json.dumps(payload), headers=headers)

and now i'm good!

现在我很好!