繁体   English   中英

github 操作存储库调度“消息”:“解析 JSON 的问题”

[英]github actions repository dispatch "message": "Problems parsing JSON"

我正在尝试创建一个可以从外部事件触发的操作工作流。 经过研究,发现我可以通过repository_dispatch 做到这一点。

我需要通过 python 触发操作,但我收到此错误和状态码 400:

{'message':'解析 JSON 的问题','documentation_url':'https://docs.github.com/rest/reference/repos#create-a-repository-dispatch-event'}

然后我使用 json.dumps() 将有效负载字典序列化为字符串,并将错误更改为代码 422:

{'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'}

这是我从文档中提供的 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)

当我尝试运行此处提供的 curl 命令时 - https://docs.github.com/en/rest/repos/repos/repos#create-a-reposi

{'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}}'

你们知道我在这里做错了吗? 或者存储库调度是否损坏? 任何帮助,将不胜感激。

你不能从字面上提供

-H "Authorization: token <TOKEN>" \

作为您的 POST 请求的一个元素。

文档邀请您注册并请求个人令牌,然后将其值插入该<TOKEN>占位符。 这有点像看到一个以Dear <insert-your-name-here>:开头的套用信函: -- 我们预计将用一个合理的值代替占位符。

另一位用户帮助我回答了这个问题-

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

json.dumps() 需要一个 Python 数据结构,将其序列化为 JSON,并将其作为字符串返回。 requests.post() 的 json 参数需要一个 Python 数据结构,将其序列化为 JSON,并将其作为帖子正文发送。 似曾相识? 结果是对于包含 JSON 的字符串,您会得到 JSON。

删除 payload = json.dumps(payload) 行,它应该可以工作。

我正在尝试打印(resp.json()),因为我收到了 json 错误响应,但是在成功发布请求时,没有响应,并且只是收到 JSON 解码器错误。 这误导了我,因为我假设一些 json 数据将在正面案例的响应中被发回,因为它是负面案例。 后来当我检查我的操作日志时,它被触发了。

回溯(最后一次调用):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)

在处理上述异常的过程中,又出现了一个异常:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM