Hey I am trying to import data that is already formatted as JSON data. I am trying to get it to be read in Python so I can use it for a http post request. I have tried saving it as .JSON and .txt and using json.dumps on both files but I'm still getting it in the wrong format. The code is below. I am guessing it is reading in the wrong format as the response from the post is getting an error. However when I use postman for the job, no error.
workingFile = 'D:\\test.json'
file = open(workingFile, 'r')
read = [file.read()]
data = json.dumps(read)
url = 'http://webaddress'
username = 'username'
password = 'password'
requestpost = requests.post(url, data, auth=(username, password))
workingFile = 'D:\\test.json'
with open(workingFile, 'r') as fh:
data = json.load(fh)
url = 'http://webaddress'
username = 'username'
password = 'password'
requestpost = requests.post(url, json=data, auth=(username, password))
By specifying json=data, requests encodes the payload as json instead of form data
To read json data from file
Parsing values from a JSON file using Python?
To read json data from string
Convert string to JSON using Python
Related
I am trying to send a json file to a url (post). I am using data parameter to open the file and send it to the url:
r = requests.post(url_info, data=open(f, 'rb'), headers=headers, verify=False)
This works right now but I have been asked to use "json" insteand of "data" to send the file.
I have seen some examples using json created as dictionaries where this is working but I am not able to make it work from a file.
I have tried to use it directly:
json=open(f, 'rb')
In f I have the route to the json file.
Serialize the json file with json.dumps(open.... But I always get a message telling me that
Try using the json module to load JSON data from the file.
import requests
import json
# open the file and load JSON data from it
with open(f, "r") as file:
data = json.load(f) # type(data) -> <class 'dict'>
# send POST request with the loaded data
r = requests.post(url_info, data=data, headers=headers, verify=False)
Need to have JSON file contents inserted inside {} for Payload. Unable to do this successfully. Any thoughts?
Attempted to write the JSON file contents as a string, this failed. Attempted to insert JSON file into payload = {}, failed.
import requests, meraki, json, os, sys
with open('networkid.txt') as file:
array = file.readlines()
for line in array:
line = line.rstrip("\n")
url = 'https://api.meraki.com/api/v0/networks/%s/alertSettings' %line
payload = {}
headers = { 'X-Cisco-Meraki-API-Key': 'API Key','Content-Type': 'application/json'}
response = requests.request('PUT', url, headers = headers, data = payload, allow_redirects=True, timeout = 10)
print(response.text)
I am writing a script to deploy parameters to Meraki networks via API. I have the JSON information formatted correctly and in its own file and what I am needing is to insert the JSON data into the location of Payload in the script. Any ideas on how to do this? I already have a for loop which is necessary to run a list of network ID's contained in a .txt file. Any thoughts?
The data parameter in requests.request takes (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the :class:Request.
You can convert your properly formatted json file to a python dictionary using json.load:
with open('json_information.json') as f:
payload = json.load(f)
Then you can directly pass data=payload into the call to requests.request:
with open('networkid.txt') as file:
array = file.readlines()
for line in array:
line = line.rstrip("\n")
url = 'https://api.meraki.com/api/v0/networks/%s/alertSettings' % line
headers = { 'X-Cisco-Meraki-API-Key': 'API Key','Content-Type': 'application/json'}
response = requests.request('PUT',
url,
headers=headers,
data=payload,
timeout = 10) # allow_redirects is True by default
print(response.text)
I'm trying to upload a file to server and I get this error:
ValueError: cannot encode objects that are that are not 2-tuples
The code:
import requests
from StringIO import StringIO
buffer = StringIO()
url = 'http://example.com/files/'
user, password = 'ex', 'ample'
buffer.write(open(r'C:\Users\example\Desktop\code\de.txt','rb').read())
r = requests.post(url, auth=(user, password), files=buffer.getvalue())
I tried auth=HTTPBasicAuth(user, pass) but that didn't work either. What can be the solution?
files only accepts a dictionary or sequence of (key, value) tuples. Give your file field a name:
r = requests.post(url, auth=(user, password), files={'file': buffer.getvalue()})
What name exactly depends on the API you are posting to. This encodes the data to a multipart/form-data encoded POST body. See POST a Multipart-Encoded file section of the requests quickstart documentation.
If you needed to post the file data as the sole data in the body, then use the data keyword argument:
r = requests.post(url, auth=(user, password), data=buffer.getvalue()})
I'm not sure why you are using a StringIO buffer; requests can handle a file handle directly, without having to read everything into memory up front. Even if you do, you'd pass in the open(....).read() result in directly without going through an in-memory buffer first; that's just overkill here.
I'd just open the file and have requests read and stream that for us:
with open(r'C:\Users\example\Desktop\code\de.txt','rb') as filedata:
r = requests.post(url, auth=(user, password), files={'file': filedata})
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...
Writing data with JSON was deprecated for performance reasons and has since been removed.
See GitHub issue comments 107043910.
I'm trying to upload a file to the Folder object in salesforce using python and simple_salesforce. The file is uploaded but is empty. can anyone tell me why and how to fix the problem? Thanks.
import base64
import json
from simple_salesforce import Salesforce
userName = 'username'
passWord = 'password'
securitytoken = 'securitytoken'
sf=Salesforce(username='userName', password='passWord', security_token='securitytoken', sandbox = True)
sessionId = sf.session_id
body = ""
with open("Info.txt", "r") as f:
body = base64.b64encode(f.read())
response = requests.post('https://cs17.my.salesforce.com/services/data/v23.0/sobjects/Document/',
headers = { 'Content-type': 'application/json', 'Authorization':'Bearer %s' % sessionId},
data = json.dumps({
'Description':'Information',
'Keywords':'Information',
'FolderId': '00lg0000000MQykAAG',
'Name': 'Info',
'Type':'txt'
})
)
print response.text
Based on the code above, your request is incomplete. You create the body value as the b64encoding iof the file you wish to upload, but it isn't included in your data associated with a 'body' key.
Depending on your version of json, you may experience some problems getting the output to play nice with Salesforce. I switched from json to simplejson and that worked better for me.