1

我正在尝试通过 GitHub API 创建一个 webhook。文档说我需要提供一个参数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

4

1 回答 1

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请求类型(动词)

于 2019-05-28T08:40:28.520 回答