I'm trying to query API to get a JSON response, the following is my code.
import requests
query_url = 'https://IP-Address/api/v1/request'
data = {"token":"8f86788b04637676b1f66eed902", "query":"days>50", "type":"json", "size":10}
response = requests.post(query_url, json=data, verify=False)
print(response.json())
I'm getting the following error,
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I also have a option to have CSV as "type" instead of JSON.
I created this Python request from cURL, which works good and outputs a JSON data. The following is the cURL command.
curl -kv -H 'Content-Type: application/json' 'https://IP-Address/api/v1/request' -d '{"token":"64bd230a775656a739b4673eb18", "query":"days>50", "type":"json", "size":10}'
Any suggestions please?
Related
I am trying to convert a cURL request into a python POST request.
Here is the cURL:
curl -X POST -F xls_file=#/path/to/form.xls https://api.ona.io/api/v1/forms
And here is the python script that I've been working on:
with open(param_build_dir+'panther_test.xls', 'rb') as form:
# xls = form.read()
response = requests.post(ona_post_base_api, params={'xls_file': form}, headers = headers, auth=HTTPBasicAuth(username, password))
print(response.status_code)
print(response.reason)
print(response.text)
When we uncomment the part of xls = form.read(), the following error occured:
Status code: 400
Reason: Bad Request
Text:
{"type":"alert-error","text":"["XLSForm not provided, expecting
either of these params: 'xml_file', 'xls_file', 'xls_url', 'csv_url',
'dropbox_xls_url', 'text_xls_form', 'floip_file'"]"}
Once commented, we receive the following error:
Status Code: 414, Request-URI Too Large
I think
import requests
files = {
'xls_file': open('/path/to/form.xls', 'rb'),
}
response = requests.post('https://api.ona.io/api/v1/forms', files=files)
should work.
resource
I need to run a curl POST to obtain API token as following:
curl - i -v -k -X POST -d '{"authType":"LOCAL","password":"mypw","username":"admin"}' --header "Content-Type:application/json" "https://x.x.x.x:xxxx/myapi_auth_path/login"
Everything runs well and I can obtain the token.
Now I want to do that with Python requests:
import requests
address = "https://x.x.x.x:xxxx/myapi_auth_path/login"
headers = {"Content-Type": "application/json"}
data = {"authType": "LOCAL",
"password": "mypw",
"username": "admin"}
res = requests.post(address, data=data, headers=headers, verify=False)
I get this error:
Expecting value: line 1 column 1 (char 0)
What is wrong with the Python code?
You're using data=data which means your data dict will be form-encoded.
You can use json=data instead (this will also set the Content-Type header for you)
It's covered in this section of the docs,
I'm making a GET request using requests package in Python 3.6 and I'm getting the above error. I tried the same request on curl and got this as a response:
{
"value": []
}
Would an empty response like this be causing the error?
You have to convert it to json
import json
response = requests.get(url)
json.loads((response.content).decode('utf-8'))
I have a flask app which looks like the following:
(note I have simplified it for the sake of this question)
#app.route("/app/ent/", methods=['POST'])
def methodpost():
req_data = request.get_json()
msg = req_data['msg']
output = jsonify(msg=msg)
return output
then for this, I have a locust file that looks like this:
from locust import HttpLocust, TaskSet, task, between
class MyClass(TaskSet):
#task(1)
def send_post(self):
self.client.headers['Content-Type'] = "application/json"
response = self.client.post("/app/ent/", json=
{
"msg": "test mesg example"
})
#temp
json_response_dict = response.json()
msg = json_response_dict['msg']
print("Post nessage returned is " + msg)
class MyTest(HttpLocust):
task_set = MyClass
wait_time = between(0.5, 3.0)
host = "http://localhost:5000"
I start locust as follows:
locust -f locust_myExample.py
Then when I run it using UI, I get following error:
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Any idea how I can print the "msg" that is returned by the flask app?
However, just to make sure it works, when I do manual test using cURL, it returns "msg"
curl --header "Content-Type: application/json" \
--request POST \
--data '{"msg":"test mesg example"}' \
http://localhost:5000/app/ent
test mesg example
The solution is:
The response returned was not json and when finally it came back as JSON, it works.
This line was missing:
output = jsonify(msg=msg)
I'm trying to pull data from a website. I'm using Python request:
users = requests.get('website name here', headers=headers).json()
I'm getting this error:
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I know that the data i'm pulling is JSON, which is one of the issues that other people were getting with this error message. My guess is, because there's a username and password required to access the site, it's giving me this error. Could that be it? If so, how can i fix that? I don't want to include my login information in my Python program.
Edit:
I now know that authorization was not the issue because i created an authorization token and i'm still getting the same issue.
request.get() returns response (Doc) object and response.content will have actual response.
I would suggest trying:
import json
import requests
headers = {...}
response = requests.get('website name here', headers=headers)
try:
users = json.loads(response.content)
except Exception as ex:
print("Error getting response")