如何使用 AJAX 在 GitHub 网络 api 的存储库上创建 webhook?
How to create a webhook on a repository on the GitHub web api using AJAX?
我正在 GitHub api 中试验 Webhooks。我通过手动完成一个工作,就像进入我的存储库并单击设置并启用网络挂钩一样。但是现在我想在 AJAX 中执行此操作,但遇到了问题。每当我尝试向网络 api 发送 POST 时,它都会失败并显示 400(错误请求)。我不确定我的代码哪里出了问题。
function createWebHooksOnRepos(token){
const webhookURL = "https://api.github.com/repos/DanoBuck/AlgorithmsAndDataStructures/hooks";
const json = {
"name": "WebHook",
"active": true,
"events": [
"issue_comment",
"issues"
],
"config": {
"url": "http://39a40427.ngrok.io/api/webhooks/incoming/github",
"content_type": "json"
}
};
$.ajax({
headers: {
"Authorization": "Token " + token
},
url: webhookURL,
data: json,
type: "POST",
dataType: "json",
success: function(data){
console.log(data);
}
});
}
谢谢
name - string - Required. Use "web" for a webhook or use the name of a valid
service. (See /hooks for the list of valid service names.)
所以在你的情况下,只需将 Webhook
重命名为 web
:
const json = {
"name": "web",
"active": true,
"events": [
"issue_comment",
"issues"
],
"config": {
"url": "http://39a40427.ngrok.io/api/webhooks/incoming/github",
"content_type": "json"
}
};
还有JSON.stringify
您发送前的数据:
$.ajax({
headers: {
"Authorization": "Token " + token
},
url: webhookURL,
data: JSON.stringify(json),
type: "POST",
dataType: "json",
success: function(data) {
console.log(data);
}
});
我正在 GitHub api 中试验 Webhooks。我通过手动完成一个工作,就像进入我的存储库并单击设置并启用网络挂钩一样。但是现在我想在 AJAX 中执行此操作,但遇到了问题。每当我尝试向网络 api 发送 POST 时,它都会失败并显示 400(错误请求)。我不确定我的代码哪里出了问题。
function createWebHooksOnRepos(token){
const webhookURL = "https://api.github.com/repos/DanoBuck/AlgorithmsAndDataStructures/hooks";
const json = {
"name": "WebHook",
"active": true,
"events": [
"issue_comment",
"issues"
],
"config": {
"url": "http://39a40427.ngrok.io/api/webhooks/incoming/github",
"content_type": "json"
}
};
$.ajax({
headers: {
"Authorization": "Token " + token
},
url: webhookURL,
data: json,
type: "POST",
dataType: "json",
success: function(data){
console.log(data);
}
});
}
谢谢
name - string - Required. Use "web" for a webhook or use the name of a valid service. (See /hooks for the list of valid service names.)
所以在你的情况下,只需将 Webhook
重命名为 web
:
const json = {
"name": "web",
"active": true,
"events": [
"issue_comment",
"issues"
],
"config": {
"url": "http://39a40427.ngrok.io/api/webhooks/incoming/github",
"content_type": "json"
}
};
还有JSON.stringify
您发送前的数据:
$.ajax({
headers: {
"Authorization": "Token " + token
},
url: webhookURL,
data: JSON.stringify(json),
type: "POST",
dataType: "json",
success: function(data) {
console.log(data);
}
});