1

enter image description here

enter image description here

How to transfer payload as a string in requests python?

my code:

def send():
    url = "url"

    cookies = {
        "csrf_token":"",
        "refresh_token":"",
        "access_token":""
    }

    data = "id%5B%5D=52626995&id%5B%5D=52627067&result_element%5BNAME%5D=%D0%90%D1%80%D0%BA%D0%B0%D0%B4%D0%B8%D0%B9&result_element%5BMAIN_USER_ID%5D=8272126&result_element%5BTAGS%5D%5B%5D=559091&result_element%5BTAGS%5D%5B%5D=559091&result_element%5Bcfv%5D%5B664393%5D%5B%5D=%7B%22DESCRIPTION%22%3A%22WORK%22%2C%22VALUE%22%3A%2271111111111%22%7D&result_element%5Bcfv%5D%5B664393%5D%5B%5D=%7B%22DESCRIPTION%22%3A%22WORK%22%2C%22VALUE%22%3A%2271111111111%22%7D&result_element%5Bcfv%5D%5B1262415%5D=12&result_element%5Bcfv%5D%5B1256527%5D=3&result_element%5Bcfv%5D%5B1272573%5D=817683&result_element%5BLEADS%5D%5B%5D=36375665&result_element%5BID%5D=52627067"
    resp = requests.post(url=url, cookies=cookies, data=data)

    return resp

But i got error cause data must be dict

Roman Pavelka
  • 3,736
  • 2
  • 11
  • 28
kirastel
  • 39
  • 5
  • My answer below should be correct to your question but looking to your data and "form data" in the image, you are maybe asking wrong question. Form data are not sent in body (payload) but as params passed as part of URL, check out this; https://stackoverflow.com/q/25385559/12118546 and https://requests.readthedocs.io/en/latest/user/quickstart/#passing-parameters-in-urls – Roman Pavelka Aug 26 '22 at 01:44

2 Answers2

1

Check out the requests documentation

data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.

So only thing you need is to encode your string to bytes:

resp = requests.post(url=url, cookies=cookies, data=data.encode())

The more detailed explanation is that strings in Python 3 are abstract representation of the characters you see. You can use Czech letter "Ř" in Python regardless what encoding is used to represent the characters as bytes in the computer memory.

However, to send it over internet, you have to send it encoded as bytes.

If you would like to get those bytes, you have to specify the encoding that converts characters to bytes representing them. For Czech, the most appropriate is UTF-8 (as for almost anything) or maybe Windows-1250 aka CP-1250:

>>> x = "Ř" 
>>> x
'Ř'
>>> x.encode("utf-8")
b'\xc5\x98'
>>> x.encode("cp1250")
b'\xd8'

Plain str.encode without encoding specified uses UTF-8, probably the best choice:

>>> x.encode()
b'\xc5\x98'
Roman Pavelka
  • 3,736
  • 2
  • 11
  • 28
1

They are right. It has to be a dictionary. The data you have here is also an encoded dictionary. Using (https://www.url-encode-decode.com/) you can understand your data better.

id[]=52626995
id[]=52627067
result_element[NAME]=Аркадий
result_element[MAIN_USER_ID]=8272126
result_element[TAGS][]=559091
result_element[TAGS][]=559091
result_element[cfv][664393][]={"DESCRIPTION":"WORK","VALUE":"71111111111"}
result_element[cfv][664393][]={"DESCRIPTION":"WORK","VALUE":"71111111111"}
result_element[cfv][1262415]=12
result_element[cfv][1256527]=3
result_element[cfv][1272573]=817683
result_element[LEADS][]=36375665
result_element[ID]=52627067

As a normal python dictionary, this is the following

{
  "id": [52626995, 52627067],
  "result_element": {
    "NAME": "Аркадий",
    "MAIN_USER_ID": 8272126,
    "TAGS": [559091, 559091],
    "cfv": {
      664393: [
        {"DESCRIPTION":"WORK","VALUE":"71111111111"}, 
        {"DESCRIPTION":"WORK","VALUE":"71111111111"}],
      1262415: 12,
      1256527: 3,
      1272573: 817683,
    },
    "LEADS": [36375665],
    "ID": 52627067
  }
}

So if you have the following code, it should work:

url = "url"

    cookies = {
        "csrf_token":"",
        "refresh_token":"",
        "access_token":""
    }

    data = {"id": [52626995, 52627067],
            "result_element": {
              "NAME": "Аркадий",
              "MAIN_USER_ID": 8272126,
              "TAGS": [559091, 559091],
              "cfv": {
                664393: [
                  {"DESCRIPTION":"WORK","VALUE":"71111111111"}, 
                  {"DESCRIPTION":"WORK","VALUE":"71111111111"}],
                1262415: 12,
                1256527: 3,
                1272573: 817683,
              },
              "LEADS": [36375665],
              "ID": 52627067
            }
          }
    resp = requests.post(url=url, cookies=cookies, data=data)

    return resp

I did it manually. First, check whether I have correctly parsed your string.

Reto Weber
  • 48
  • 1
  • 6