简体   繁体   中英

send payload in requests python

在此处输入图像描述

在此处输入图像描述

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

Check out therequests 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.

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

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

However, to send it over internet, you have to specify the encoding to be used and plain str.encode without encoding uses UTF-8, probably the best choice:

>>> x.encode()
b'\xc5\x98'

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM