我正在尝试通过GitHub API创建一个Webhook。 docs说我需要提供一个config
参数,该参数应该是一个对象,但是我不确定如何在URL参数中发送JSON。这是我尝试过的:
fetch(`https://api.github.com/repos/${repo.full_name}/hooks?config={"url": "https://webhooks.example.com", "content_type": "json"}`, {
method: "POST",
headers: {
Accept: "application/vnd.github.v3+json",
Authorization: `token ${account.accessToken}`
}
});
和
fetch(`https://api.github.com/repos/${repo.full_name}/hooks?config.url=https://webhooks.example.com&config.content_type=json`, {
method: "POST",
headers: {
Accept: "application/vnd.github.v3+json",
Authorization: `token ${account.accessToken}`
}
});
它们都导致以下错误:
{
"message": "Validation Failed",
"errors": [
{
"resource": "Hook",
"code": "custom",
"message": "Config must contain URL for webhooks"
}
],
"documentation_url": "https://developer.github.com/v3/repos/hooks/#create-a-hook"
}
如何正确发送JSON对象?我正在寻找使用node-fetch
答案 0 :(得分:1)
在执行发布请求时,这意味着将有一个有效负载,而您正在使用的库将期望包含您的有效负载的body
属性。
所以只需添加
fetch('https://api.github.com/repos/${repo.full_name}/hooks') {
method: "POST",
headers: {
Accept: "application/vnd.github.v3+json",
Authorization: `token ${account.accessToken}`
},
body:JSON.stringify(yourJSON) //here this is how you send your datas
});
然后node-fetch
会将您的请求发送给您。
如果您需要更多详细信息,我会扩大答案
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods此处简要介绍了不同的http请求类型(动词)