M
M
Mamol272019-04-18 15:09:27
Python
Mamol27, 2019-04-18 15:09:27

How to send an array in a python post request?

Hello, I'm trying to use the rest api of one resource, and I can't send the array where it should be.
Request

query_pattern = {
    "key": "apikey",
    "username": "admin",
    "password": "admin",
    "action": "",
    "entity_id": "",
    "items": ""
}


def send_contract_json(contract_list):
    items = []
    for e in contract_list:
        item = {"field_454": e[0], "field_455": "-----"}
        items.append(item)

    query_pattern["action"] = "insert"
    query_pattern["entity_id"] = 42
    query_pattern["items"] = items
    print(query_pattern)

    response = requests.post(url, query_pattern)
    print(json.loads(response.text))

Answer:
Conclusion:
{'items': [{'field_454': 1, 'field_455': '-----'}, {'field_454': 2, 'field_455': '-----'}, {'field_454': 3, 'field_455': '-----'}, {'field_454': 4, 'field_455': '-----'}, {'field_454': 5, 'field_455': '-----'}, {'field_454': 6, 'field_455': '-----'}, {'field_454': 7, 'field_455': '-----'}, {'field_454': 8, 'field_455': '-----'}, {'field_454': 9, 'field_455': '-----'}, {'field_454': 10, 'field_455': '-----'}, {'field_454': 11, 'field_455': '-----'}, {'field_454': 12, 'field_455': '-----'}], 'entity_id': 42, 'action': 'insert', 'key': 'apikey', 'username': 'admin', 'password': 'admin'}
{'error_message': 'items is not array', 'status': 'error', 'error_code': ''}

Description of the rest api in php:
https://www.rukovoditel.net/ru/api.html
The question is who has the error, me or on the server side?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
alternativshik, 2019-04-18
@Mamol27

There they have http_build_query, so I advise you to try something like this.

import urllib.parse

data = urllib.parse.urlencode(query_pattern)
requests.post(url, data=data)

Y
Yuri, 2019-09-11
@Fearta

Good afternoon, did you solve the problem? very interesting. Half a year ago I tried to solve it but nothing came of it. (I attracted 2 programmers, and even the creator of the "Head" constructor) but there is no answer (

G
gerladeno, 2021-06-06
@gerladeno

Good afternoon!
I stumbled on this for a while myself, but it seems like json.dumps solves the problem, no?

...
payload = [{'id': 1, 'value': 11}, {'id': 2, 'value': 22}, {'id': 3, 'value': 33}]
data = json.dumps(payload)
result = requests.post(url, data=data)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question