[英]github actions repository dispatch "message": "Problems parsing JSON"
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.
经过研究,发现我可以通过repository_dispatch 做到这一点。
I need to trigger the actions through python but I get this error and status code 400:我需要通过 python 触发操作,但我收到此错误和状态码 400:
{'message': 'Problems parsing JSON', 'documentation_url': 'https://docs.github.com/rest/reference/repos#create-a-repository-dispatch-event'}
{'message':'解析 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:然后我使用 json.dumps() 将有效负载字典序列化为字符串,并将错误更改为代码 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'}
{'message': '无效请求。\n\n对于 'links/0/schema', "{"event_type": "test", "client_payload": {"unit": false, "integration": true}}"不是 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这是我从文档中提供的 curl 示例构造的代码片段
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:当我尝试运行此处提供的 curl 命令时 - https://docs.github.com/en/rest/repos/repos/repos#create-a-reposi
{'message': 'Problems parsing JSON', 'documentation_url': 'https://docs.github.com/rest/reference/repos#create-a-repository-dispatch-event'}
{'message':'解析 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.
任何帮助,将不胜感激。
You cannot literally offer你不能从字面上提供
-H "Authorization: token <TOKEN>" \
as an element of your POST request.作为您的 POST 请求的一个元素。
The documentation is inviting you to signup and request a personal token, and then insert its value into that <TOKEN>
placeholder.该文档邀请您注册并请求个人令牌,然后将其值插入该
<TOKEN>
占位符。 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.这有点像看到一个以
Dear <insert-your-name-here>:
开头的套用信函: -- 我们预计将用一个合理的值代替占位符。
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.
json.dumps() 需要一个 Python 数据结构,将其序列化为 JSON,并将其作为字符串返回。 The json parameter for requests.post() expects a Python data structure, serializes that as JSON, and sends it as the post body.
requests.post() 的 json 参数需要一个 Python 数据结构,将其序列化为 JSON,并将其作为帖子正文发送。 Seems familiar?
似曾相识? The result is that you get JSON for a string that contains JSON.
结果是对于包含 JSON 的字符串,您会得到 JSON。
Remove the payload = json.dumps(payload) line and it should work.
删除 payload = json.dumps(payload) 行,它应该可以工作。
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.我正在尝试打印(resp.json()),因为我收到了 json 错误响应,但是在成功发布请求时,没有响应,并且只是收到 JSON 解码器错误。 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.
这误导了我,因为我假设一些 json 数据将在正面案例的响应中被发回,因为它是负面案例。 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)
回溯(最后一次调用):json 中的文件“C:\Users\dcunh\Documents\Code\Test\env_test\lib\site-packages\requests\models.py”,第 971 行,返回 complexjson.loads(self. text, **kwargs) 文件“C:\Users\dcunh\AppData\Local\Programs\Python\Python39\lib\json_ init _.py”,第 346 行,加载返回 _default_decoder.decode(s) 文件“C: \Users\dcunh\AppData\Local\Programs\Python\Python39\lib\json\decoder.py",第 337 行,在解码 obj 中,end = self.raw_decode(s, idx=_w(s, 0).end( )) 文件“C:\Users\dcunh\AppData\Local\Programs\Python\Python39\lib\json\decoder.py”,第 355 行,在 raw_decode 中引发 JSONDecodeError("Expecting value", s, err.value) from无 json.decoder.JSONDecodeError:预期值:第 1 行第 1 列(字符 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)
Traceback(最近一次调用最后):文件“C:\Users\dcunh\Documents\Code\Test\test.py”,第 10 行,在 print(resp.json()) 文件“C:\Users\dcunh\Documents \Code\Test\env_test\lib\site-packages\requests\models.py",第 975 行,在 json 中引发 RequestsJSONDecodeError(e.msg, e.doc, e.pos) requests.exceptions.JSONDecodeError: Expecting value: line 1 列 1(字符 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.