0

Using python requests to talk to the github api.

response = requests.post('https://api.github.com/orgs/orgname/repos', auth, data)

If the request returns a 405 error I get HTML in the response.text

<html>
<head><title>405 Not Allowed</title></head>
<body bgcolor="white">
<center><h1>405 Not Allowed</h1></center>
</body>
</html>

If the request returns a 422 error I get JSON in the response.text

{"message":"Validation Failed","errors":        [{"resource":"Repository","code":"custom","field":"name","message":"name already exists on this account"}],"documentation_url":"https://developer.github.com/v3/repos/#create"}

Can I force the API to only return JSON?

If not, can I find out what type the response content will be?

3
  • To find what kind of response the content will be in you could check the http response code! Commented Sep 30, 2016 at 15:22
  • Also as I read here: developer.github.com/v3/#schema all data is sent and received as JSON, so in your case it may be an authentication issue - or in other words you are trying to retrieve not-allowed content and that is maybe why the server doesn't send a response back, instead prints a not allowed message in HTML ... Commented Sep 30, 2016 at 15:29
  • You can go with try: json.loads() and then except with html. Commented Sep 30, 2016 at 16:05

1 Answer 1

1

In response to:

If not, can I find out what type the response content will be?

If you want to know why github returns html rather than JSON for the API call, I can not answer that; however, to the answer to the above asked question, you can look at the "content" header of the http response.

response = requests.post('https://api.github.com/orgs/orgname/repos', auth, data)
if "application/json" in response.headers['content-type']:
    print response.json()

In response to:

Can I force the API to only return JSON? you could try forcing the "Accept header within the request; however, this is down to GitHub server if it wants to serve json in the response

response = requests.post('https://api.github.com/orgs/omnifone/repos', auth=auth, data=data, headers={"Accept": "application/json"})
Sign up to request clarification or add additional context in comments.

Comments

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.