如何通过 a API 在 Gitlab 中编辑问题?
How do I edit issues in Gitlab via the a API?
我正在尝试通过 C# 应用程序中的 API 向 Gitlab 问题添加评论。
下面的代码如下:
static async Task Test()
{
using (client)
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Private-Token", "TheToken");
StringContent content = new StringContent(@"{\""body\"": \""newnote\""}", Encoding.UTF8, "application/json");
HttpResponseMessage result = await client.PutAsync("https://URL/api/v4/projects/1/issues/1/notes", content);
string resultContentJson = await result.Content.ReadAsStringAsync();
}
}
但它会产生以下错误:
{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content:
System.Net.Http.StreamContent, Headers:\r\n{\r\n Connection:
keep-alive\r\n X-Request-Id: XXX\r\n
X-Runtime: 0.002577\r\n Date: Fri, 17 Sep 2021 15:07:23 GMT\r\n
Server: nginx\r\n Content-Length: 36\r\n Content-Type:
application/json; charset=utf-8\r\n}}
我假设我在设置内容时做错了什么,但我看不到什么。
有人有什么想法吗?
编辑:
我改变了:
StringContent content = new StringContent(@"{\""body\"": \""newnote\""}", Encoding.UTF8, "application/json");
到
StringContent content = new StringContent(@"{""body"": ""newnote""}", Encoding.UTF8, "application/json");
有效 json。它现在产生错误消息:
{StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content:
System.Net.Http.StreamContent, Headers:\r\n{\r\n Connection:
keep-alive\r\n Vary: Origin\r\n X-Content-Type-Options: nosniff\r\n
X-Frame-Options: SAMEORIGIN\r\n X-Gitlab-Feature-Category:
not_owned\r\n X-Request-Id: XXX\r\n X-Runtime: 0.012825\r\n
Cache-Control: no-cache\r\n Date: Fri, 17 Sep 2021 15:32:41 GMT\r\n
Server: nginx\r\n Content-Length: 25\r\n Content-Type:
application/json\r\n}}
原来问题出在使用
client.PutAsync
应该使用:
client.PostAsync
我正在尝试通过 C# 应用程序中的 API 向 Gitlab 问题添加评论。
下面的代码如下:
static async Task Test()
{
using (client)
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Private-Token", "TheToken");
StringContent content = new StringContent(@"{\""body\"": \""newnote\""}", Encoding.UTF8, "application/json");
HttpResponseMessage result = await client.PutAsync("https://URL/api/v4/projects/1/issues/1/notes", content);
string resultContentJson = await result.Content.ReadAsStringAsync();
}
}
但它会产生以下错误:
{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:\r\n{\r\n Connection: keep-alive\r\n X-Request-Id: XXX\r\n X-Runtime: 0.002577\r\n Date: Fri, 17 Sep 2021 15:07:23 GMT\r\n Server: nginx\r\n Content-Length: 36\r\n Content-Type: application/json; charset=utf-8\r\n}}
我假设我在设置内容时做错了什么,但我看不到什么。 有人有什么想法吗?
编辑:
我改变了:
StringContent content = new StringContent(@"{\""body\"": \""newnote\""}", Encoding.UTF8, "application/json");
到
StringContent content = new StringContent(@"{""body"": ""newnote""}", Encoding.UTF8, "application/json");
有效 json。它现在产生错误消息:
{StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:\r\n{\r\n Connection: keep-alive\r\n Vary: Origin\r\n X-Content-Type-Options: nosniff\r\n X-Frame-Options: SAMEORIGIN\r\n X-Gitlab-Feature-Category: not_owned\r\n X-Request-Id: XXX\r\n X-Runtime: 0.012825\r\n Cache-Control: no-cache\r\n Date: Fri, 17 Sep 2021 15:32:41 GMT\r\n Server: nginx\r\n Content-Length: 25\r\n Content-Type: application/json\r\n}}
原来问题出在使用
client.PutAsync
应该使用:
client.PostAsync