how to convert curl --data-binary to python request?

Multi tool use
Multi tool use


how to convert curl --data-binary to python request?



I have the curl command:


curl 'https://example.com/submit'
-H 'Accept: */*'
-H 'Referer: http://www.example.com/go/'
-H 'Origin: http://www.example.com'
-H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
-H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryFkQeCsmEXjVI5SJ0'
--data-binary $'------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rnContent-Disposition: form-data; name="firstName"rnrOlegrn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rnContent-Disposition: form-data; name="campaignCode"rnrnrn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rnContent-Disposition: form-data; name="validation"rnrn{"firstName":"required"}rn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0--rn'
--compressed



with:


--data-binary $'------WebKitFormBoundary...'



but my converted version to python code does not work


import requests

headers = {
'Accept': '*/*',
'Referer': 'http://www.example.com/go',
'Origin': 'http://www.example.com',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryFkQeCsmEXjVI5SJ0'
}

data = '$------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rnContent-Disposition: form-data; name="firstName"rnrOlegrn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rnContent-Disposition: form-data; name="campaignCode"rnrnrn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rnContent-Disposition: form-data; name="validation"rnrn{"firstName":"required"}rn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0--rn'

response = requests.post('https://example.com/submit', headers=headers,
data=data)



Maybe I'm missing something? how to convert --data-binary $?


--data-binary $



The curl command returns: {"success":true,"count":1}, but pycode returns: {"success":false,"count":1}


{"success":true,"count":1}


{"success":false,"count":1}



Also errors:



Without $ in datastring response-{"success":false,"count":0}.


$


data


{"success":false,"count":0}



Using a bytes instead of a str for data.


bytes


str


data



UPDATES:



this curl command works with curl 7.58.0 on my linux, but on windows- cmd command returns the same error as in the pycode without $


$





What does "does not work" mean? What happens? And, given that the headers aren't exactly the same, why are they different, and why are you sure that doesn't matter?
– abarnert
Jul 3 at 0:33





Anyway, if you want to specify the exact binary bytes, you can use a bytes instead of a str for data, which seems to be what you're asking for. But, given that your string is pure ASCII, I can't imagine that it would make a difference.
– abarnert
Jul 3 at 0:35


bytes


str


data





@abarnert I corrected headers. when i'm using the curl command server returns {"success":true}, but if pycode {"success":false} yes, bytes instead of str dont resolve my problem.
– Олег Кирьянов
Jul 3 at 0:54



bytes


str





Why are you including the $ in your requests data (not sure why you need it in your cURL call either, given that it's not expanding into anything)?
– zwer
Jul 3 at 1:04



$


requests


cURL





@zwer bcse curl.trillworks.com converted with $, anyway this fix cant help me
– Олег Кирьянов
Jul 3 at 1:46


$




2 Answers
2



The payload of a captured packet of your "curl" version :


MIME Multipart Media Encapsulation, Type: multipart/form-data, Boundary: "----WebKitFormBoundaryFkQeCsmEXjVI5SJ0"
[Type: multipart/form-data]
First boundary: ------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rn
Encapsulated multipart part:
Content-Disposition: form-data; name="firstName"rn
Data (6 bytes)
Data: 0d5c4f6c6567
[Length: 6]
Boundary: rn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rn
Encapsulated multipart part:
Content-Disposition: form-data; name="campaignCode"rnrn
Boundary: rn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rn
Encapsulated multipart part:
Content-Disposition: form-data; name="validation"rnrn
Data (24 bytes)
Data: 7b2266697273744e616d65223a227265717569726564227d
[Length: 24]
Last boundary: rn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0--rn



The payload of a captured packet of your "python" version :


MIME Multipart Media Encapsulation, Type: multipart/form-data, Boundary: "----WebKitFormBoundaryFkQeCsmEXjVI5SJ0"
[Type: multipart/form-data]
Preamble: 242d2d2d2d2d2d5765624b6974466f726d426f756e646172...
First boundary: ------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rn
Encapsulated multipart part:
Content-Disposition: form-data; name="campaignCode"rnrn
Boundary: rn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rn
Encapsulated multipart part:
Content-Disposition: form-data; name="validation"rnrn
Data (24 bytes)
Data: 7b2266697273744e616d65223a227265717569726564227d
[Length: 24]
Last boundary: rn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0--rn



The first entry seem to be not recognized normaly.



You should remove "$" from head of variable data in python code.


"$"


data


data = '------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rnContent-Disposition: form-data; name="firstName"rnrOlegrn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rnContent-Disposition: form-data; name="campaignCode"rnrnrn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rnContent-Disposition: form-data; name="validation"rnrn{"firstName":"required"}rn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0--rn'



Then, the payload of a captured packet of your new "python" version:


MIME Multipart Media Encapsulation, Type: multipart/form-data, Boundary: "----WebKitFormBoundaryFkQeCsmEXjVI5SJ0"
[Type: multipart/form-data]
First boundary: ------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rn
Encapsulated multipart part:
Content-Disposition: form-data; name="firstName"rn
Data (6 bytes)
Data: 0d5c4f6c6567
[Length: 6]
Boundary: rn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rn
Encapsulated multipart part:
Content-Disposition: form-data; name="campaignCode"rnrn
Boundary: rn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rn
Encapsulated multipart part:
Content-Disposition: form-data; name="validation"rnrn
Data (24 bytes)
Data: 7b2266697273744e616d65223a227265717569726564227d
[Length: 24]
Last boundary: rn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0--rn



This is same with "curl" version.





thanks for the answer! I tried this combination- the server returns {"success":false,"count":0} , not {"success":true,"count":1} .
– Олег Кирьянов
Jul 3 at 1:43





this curl command works with curl 7.58.0 on my linux machine, but on windows- cmd command(copied form my browser) returns the same error as in the pycode. hmm
– Олег Кирьянов
Jul 3 at 2:31




The problem is that your two commands aren't sending the same data.



Assuming you're running this in bash or a similar shell, $'-----blahblah…' doesn't mean the string '$-----blahblah…', it means the contents of the environment variable '-----blahblah…' if there is one, or the string '-----blahblah…' if there isn't. Since there almost certainly is no variable with that name, you just get the string—without the leading $.


$'-----blahblah…'


'$-----blahblah…'


'-----blahblah…'


'-----blahblah…'


$



But in Python, you're including the $ as part of the data. This means you've got an extra entry, the $, before the first multipart boundary.


$


$



To fix that, just don't do that. Instead of this:


data = '$------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rnContent-Disposition: form-data; name="firstName"rnrOlegrn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rnContent-Disposition: form-data; name="campaignCode"rnrnrn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rnContent-Disposition: form-data; name="validation"rnrn{"firstName":"required"}rn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0--rn'



… do this:


data = '------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rnContent-Disposition: form-data; name="firstName"rnrOlegrn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rnContent-Disposition: form-data; name="campaignCode"rnrnrn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0rnContent-Disposition: form-data; name="validation"rnrn{"firstName":"required"}rn------WebKitFormBoundaryFkQeCsmEXjVI5SJ0--rn'





thx for the answer, but this fix also returns False and count 0 instaed of 1. {"success":false,"count":0}
– Олег Кирьянов
Jul 3 at 1:20






i think the server means string-counts
– Олег Кирьянов
Jul 3 at 1:25






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

4F7w4GeNC2rQpyB EGJi
M0D4YBJl9t9aNcH NwHA MQpixQH4jSX96jrS8ird4KiYm,wfNCtN8mLkK2HE,cgh,vJrU4EmHbT,y5vYR,l2N,rDf7C T t2u 7tG4JIGFM4u

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications