2

I essentially want to do this:

curl -X POST "http://URL?u=user&p=password" --data-binary @myconfig.json

with urllib2.

I found examples for sending just the user and password, or just the binary, but not both at the same time, and some contradict each other.

I am doing this to create an influxdb with a retention policy based on instructions here:

https://docs.influxdata.com/influxdb/v0.8/advanced_topics/sharding_and_storage/

curl -X POST "http://localhost:8086/cluster/database_configs/mydb?u=root&p=root" --data-binary @myconfig.json

Thanks!

1 Answer 1

3
import urllib2
import json

url = 'http://url.com?u=user&p=pass'
data = json.dumps({'config':'configData'}) # your JSON File goes in here, as argument to dumps.
cont_len = len(data)
req = urllib2.Request(url, data, {'Content-Type': 'application/json', 'Content-Length': cont_len})
f = urllib2.urlopen(req)
response = f.read()
f.close()

That solves it!

Note that, with urllib2, you cannot specify the .json file. You simply put its content into the json.dumps function.

Sign up to request clarification or add additional context in comments.

4 Comments

doesn't work, says HTTP Error 400: Bad Request I'm going to add more info to the question.
Okay @GilZellner. Please do.
scratch that, looks like the mistake was on my end in the url creation. Thanks!
This works because the data in question isn't really binary data, it's json, which is a string. If it was bytes, this wouldn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.