I'm trying to post a json file to influxdb on my local host. This is the code:
import json
import requests
url = 'http://localhost:8086/write?db=mydb'
files ={'file' : open('sample.json', 'rb')}
r = requests.post(url, files=files)
print(r.text)
This is what sample.json
looks like:
{
"region" : "eu-west-1",
"instanceType": "m1.small"
}
My response gives the following errors:
{"error":"unable to parse '--1bee44675e8c42d8985e750b2483e0a8\r':
missing fields\nunable to parse 'Content-Disposition: form-data;
name=\"file\"; filename=\"sample.json\"\r': invalid field
format\nunable to parse '\r': missing fields\nunable to parse '{':
missing fields\nunable to parse '\"region\" : \"eu-west-1\",': invalid
field format\nunable to parse '\"instanceType\": \"m1.small\"': invalid
field format\nunable to parse '}': missing fields"}
My json seems to be a valid json file. I am not sure what I am doing wrong.
I think that the fault maybe is that you just open the file but not read it. I mean since you want to post the content of the json
object which is stored on the file, and not the file itself, it may be better to do that instead:
import json
import requests
url = 'http://localhost:8086/write?db=mydb'
json_data = open('sample.json', 'rb').read() # read the json data from the file
r = requests.post(url, data=json_data) # post them as data
print(r.text)
which is actually your code modified just a bit...