简体   繁体   中英

How do I write this curl with binary data to python?

I have this curl that I am trying to make work in python. My problem is, that there is a --data-binary $'{\"streamerId\":\"u75aa923027266ec9ec4f0857ef0e22d3\"}' \ which I don't know how to POST.

The curl which works is:

curl -i -s -k -X $'POST' \
    -H $'Accept-Language: en' -H $'User-Agent: CastService/2.8.2 Android/6.0 Burp_Pen_Testing' -H $'X-Line-Live-AccessToken: dAGvdW_0jh1zyPo5oxIOoUtQ9wN__Z_QpbB0WicyRRTc0BM_D55vRUSu1K3wtZzYwgjzeSuVAAABcoThJOM' -H $'X-Line-Live-PushToken: d34hWbzL4bA:APA91bF_dMmD1WSWuCtBIxnMoeO1fo8fUQIACsUu2nEwQrl-ZsWvB45WA2tk4gpGSf9V9Ep4dyhdj94b0dSKv2RknDYBDuJU_Qa1X7wSqgWiOrDcxDGzAGGORC5v2md7rG-0iaSSW8VN' -H $'X-Line-Live-PushType: GCM' -H $'X-Line-Live-PushSetting: allMessages' -H $'X-Line-Live-TimeZoneId: America/New_York' -H $'X-Line-Live-Country: DK' -H $'X-Line-Live-Adid: 8dee9a2c-29b2-4da8-b76f-ab19f7ef4df2' -H $'Content-Type: application/json; charset=UTF-8' -H $'Content-Length: 50' -H $'Host: api.linelive.me' -H $'Connection: close' -H $'Accept-Encoding: gzip, deflate' \
    --data-binary $'{\"streamerId\":\"u75aa923027266ec9ec4f0857ef0e22d3\"}' \
    $'https://api.linelive.me/api/v1.4/user/follow'

I put it through an automatic curl to python and got:

import requests

headers = {
    'Accept-Language': 'en',
    'User-Agent': 'CastService/2.8.2 Android/6.0 Burp_Pen_Testing',
    'X-Line-Live-AccessToken': 'dAGvdW_0jh1zyPo5oxIOoUtQ9wN__Z_QpbB0WicyRRTc0BM_D55vRUSu1K3wtZzYwgjzeSuVAAABcoThJOM',
    'X-Line-Live-PushToken': 'd34hWbzL4bA:APA91bF_dMmD1WSWuCtBIxnMoeO1fo8fUQIACsUu2nEwQrl-ZsWvB45WA2tk4gpGSf9V9Ep4dyhdj94b0dSKv2RknDYBDuJU_Qa1X7wSqgWiOrDcxDGzAGGORC5v2md7rG-0iaSSW8VN',
    'X-Line-Live-PushType': 'GCM',
    'X-Line-Live-PushSetting': 'allMessages',
    'X-Line-Live-TimeZoneId': 'America/New_York',
    'X-Line-Live-Country': 'US',
    'X-Line-Live-Adid': '8dee9a2c-29b2-4da8-b76f-ab19f7ef4df2',
    'Content-Type': 'application/json; charset=UTF-8',
    'Content-Length': '50',
    'Host': 'api.linelive.me',
    'Connection': 'close',
    'Accept-Encoding': 'gzip, deflate',
}

data = '{\\"streamerId\\":\\"u75aa923027266ec9ec4f0857ef0e22d3\\"}'

response = requests.post('https://api.linelive.me/api/v1.4/user/follow', headers=headers, data=data, verify=False)

I've tried to change things like removing the "http://$" and changing the data string in various ways. But I can't seem to get it right, why I now ask you for help.

In advance, thank you for your kind help.

If you are trying to pass a JSON value in the payload, use the json keyword argument:

# A plain dict; requests will serialize it to JSON for you
data = {"streamerId": "u75aa923027266ec9ec4f0857ef0e22d3"}

response = requests.post(
    'https://api.linelive.me/api/v1.4/user/follow', 
    headers=headers,
    , 
    verify=False
)

Using json is short for data=json.dumps(data) .

chepner's answer is correct but if you want another alternative for some reason, remove the escaping from your data string

data = '{"streamerId": "u75aa923027266ec9ec4f0857ef0e22d3"}'

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