I would like to create a python script that makes a series of web requests to endpoints that will spit out JSON. After receiving the response, I want to validate that it is well-formed JSON, and not some error page. Also, I will need to insert an API key into the request header to get a proper response. what is the best way to go about doing this in python? Thanks!
-
1What's the specific question here? What have you tried already? What level of validation do you need? Would a value error thrown by json.loads be enough?ModulusJoe– ModulusJoe2013-10-15 22:46:28 +00:00Commented Oct 15, 2013 at 22:46
-
The main questions are: 1. how do i make a request 2. how do I insert the api key into that request. 3. how do i know that the JSON is well-formed. Specifically, I only care that it conforms to the JSON standard, and can be deserialized into some arbitrary object. I am NOT trying to validate against a particluar schema, (nor do i want to).Nick– Nick2013-10-16 14:28:20 +00:00Commented Oct 16, 2013 at 14:28
Add a comment
|
2 Answers
To validate the JSON you can use http://python-jsonschema.readthedocs.org/en/latest/
To insert the API key in the header you can use Requests http://docs.python-requests.org/en/latest/user/quickstart/
Comments
Validate the json use json.dump(), if your json object incorect it's return the error, handle the error using try catch and answer to your second question , i think you talking about X-AUTH-TOKEN , if your request return json object use this header.
headers = {
"Content-Type": "application/json",
"X-AUTH-TOKEN": your API Token
}
otherwise use
"Content-type":"application/x-www-form-urlencoded"