Joel Pou
Joel Pou

Reputation: 158

Curl --data-binary equivalent in python-requests library

I'm trying to post testing data to a server by using python-requests library in python. I am able to post data successfully with the following command using Curl in the terminal:

curl -i -XPOST 'http://myServerAddress/write?db=some_data' --data-binary 'param1,state=test,param2=1 param3=2.932,param4=3250 1497064544944 '

I'm trying to do the same thing with requests or maybe even pycurl python library. I am having a hard time translating the "--data-binary" part with pycurl or requests. Doing something like this with requests library for example:

import requests    

p = requests.post('http://myServerAddress/write?db=some_data', data={'param1,state=test,param2=1 param3=2.932,param4=3250 1497064544944 '})

print(p)
print(p.status_code)
print(p.text)

Getting "TypeError: a bytes-like object is required, not 'set'" in the shell when I run the code. What am I missing? Any help is appreciated. Thanks.

Upvotes: 7

Views: 7689

Answers (1)

Junyong Yao
Junyong Yao

Reputation: 699

Try something like this

import requests
data='param1,state=test,param2=1 param3=2.932,param4=3250 1497064544944 '
p = requests.post('http://myServerAddress/write?db=some_data', data.encode())

Upvotes: 7

Related Questions