1

Im trying to use a simple(er) Python 3.5 script to allow me to change the status of Pull Requests on GitHub, however I am getting a rather strange error (because I tested the code with curl and had no issues)

Here's the relevant part of my code:

payload = {"state": "success", "target_url": "https://github.com/Electromaster232/FireSurvival-Docs", "description": "hi", "context": "pullrequest"}

print(payload)
headers = {"Authorization": "token <token removed>", "Host": "api.github.com", "Content-Type": "application/json"}

res = requests.post(url, data=payload, headers=headers)
print(res.text)

This returns:

<Response [400]>
{"message":"Problems parsing JSON","documentation_url":"https://developer.github.com/v3/repos/statuses/#create-a-status"}

After looking at similar questions, I don't see an answer that helped.

Kalamarico
  • 5,466
  • 22
  • 53
  • 70
  • could you include the URL you're making the request to? – Borko Kovacev Oct 01 '17 at 21:34
  • @BorkoKovacev URL will change cause it changes per PR, but for the one I was testing: https://api.github.com/repos/Electromaster232/FireSurvival-Docs/statuses/f781fdb6aeeae1e328bec85b816046b2d4a01a1f – Electromaster Oct 01 '17 at 22:02
  • could you try changing `data` to `json` in the `post()` and try like that? – Borko Kovacev Oct 02 '17 at 00:19
  • Sorry, that's not what I meant. I meant instead of `data` argument, use `json` argument. So `res = requests.post(url, json=payload, headers=headers)` – Borko Kovacev Oct 02 '17 at 00:39
  • 1
    Lol, I figured out what you meant before you said it, added it as an answer, thanks man! @BorkoKovacev – Electromaster Oct 02 '17 at 00:42
  • No problem, glad it helped! There seems to be an interesting correlation with `json` argument as it sets the headers and encodes it properly. For reference https://stackoverflow.com/questions/9733638/post-json-using-python-requests and http://docs.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests – Borko Kovacev Oct 02 '17 at 00:44

1 Answers1

0

Originally answered by @BorkoKovacev

Updating data to json does the trick

res = requests.post(url, json=payload, headers=headers)

Is correct