Home > Software engineering >  github actions repository dispatch "message": "Problems parsing JSON"
github actions repository dispatch "message": "Problems parsing JSON"

Time:08-04

I am trying to create an actions workflow that can be triggered from an external event. Upon researching, found out I can do it through repository_dispatch.

I need to trigger the actions through python but I get this error and status code 400:

{'message': 'Problems parsing JSON', 'documentation_url': 'https://docs.github.com/rest/reference/repos#create-a-repository-dispatch-event'}

I then serialized the payload dictionary to string with json.dumps() and the error changed to with code 422:

{'message': 'Invalid request.\n\nFor 'links/0/schema', "{"event_type": "test", "client_payload": {"unit": false, "integration": true}}" is not an object.', 'documentation_url': 'https://docs.github.com/rest/reference/repos#create-a-repository-dispatch-event'}

Here's my code snippet that I constructer from the curl example provided in the docs

import requests
import json

url = "https://api.github.com/repos/larwindcunha/<repo>/dispatches"
payload = {"event_type": "test", "client_payload": {"unit":False,"integration":True}}
header = {"Accept": "application/vnd.github json", "Authorization": "token <my_token>"}
payload = json.dumps(payload)
resp = requests.post(url=url, headers=header, json=payload)

When I try running the curl command provided here - https://docs.github.com/en/rest/repos/repos#create-a-repository-dispatch-event, I get the same error with code 400:

{'message': 'Problems parsing JSON', 'documentation_url': 'https://docs.github.com/rest/reference/repos#create-a-repository-dispatch-event'}

curl \
  -X POST \
  -H "Accept: application/vnd.github json" \ 
  -H "Authorization: token <MY_TOKEN_HERE>" \
  https://api.github.com/repos/OWNER/REPO/dispatches \
  -d '{"event_type":"on-demand-test","client_payload":{"unit":false,"integration":true}}'

Does you guys know if I'm doing something wrong here? Or is repository dispatch broken? Any help would be appreciated.

CodePudding user response:

You cannot literally offer

-H "Authorization: token <TOKEN>" \

as an element of your POST request.

The documentation is inviting you to signup and request a personal token, and then insert its value into that <TOKEN> placeholder. It's a bit like seeing a form letter that starts Dear <insert-your-name-here>: -- we anticipate that a sensible value will be substituted for the placeholder.

CodePudding user response:

Another user helped me with this answer -

payload = json.dumps(payload)
resp = requests.post(url=url, headers=header, json=payload)

json.dumps() expects a Python data structure, serializes that as JSON, and returns it as a string. The json parameter for requests.post() expects a Python data structure, serializes that as JSON, and sends it as the post body. Seems familiar? The result is that you get JSON for a string that contains JSON.

Remove the payload = json.dumps(payload) line and it should work.

I was trying to print(resp.json()) since I was getting a json response on error, but on successful post request, there was no response, and just got an JSON decoder error. This misled me as I assumed that some json data would be sent back in the response for positive case as it was for a negative one. Later when I checked my actions log, it was being triggered.

Traceback (most recent call last): File "C:\Users\dcunh\Documents\Code\Test\env_test\lib\site-packages\requests\models.py", line 971, in json return complexjson.loads(self.text, **kwargs) File "C:\Users\dcunh\AppData\Local\Programs\Python\Python39\lib\json_init_.py", line 346, in loads return _default_decoder.decode(s) File "C:\Users\dcunh\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Users\dcunh\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Users\dcunh\Documents\Code\Test\test.py", line 10, in print(resp.json()) File "C:\Users\dcunh\Documents\Code\Test\env_test\lib\site-packages\requests\models.py", line 975, in json raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

  • Related