新一期GitLab API有一个cURL示例:
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>"
"https://gitlab.example.com/api/v4/projects/4/issues?title=Issues%20with%20auth&labels=bug"
我使用的HttpRequestMessage方法是这样的:
static async Task<Issue> new_issue(string repository, int appId, string issueTitle, string issueDescription, int assignee_ids) //New ISSUE
{
string issue_path = which_repo(repository)[0] + "projects/" + appId + "/issues/" + "?title=" + issueTitle + "&description=" + issueDescription + "&assignee_ids=" + assignee_ids;
using (var request = new HttpRequestMessage(new HttpMethod("POST"), issue_path))
{
request.Headers.TryAddWithoutValidation("PRIVATE-TOKEN", which_repo(repository)[1]);
var response = await client.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
Issue new_issue = JsonConvert.DeserializeObject<Issue>(content);
Console.WriteLine("Utworzono issue:"+Environment.NewLine+"Tytuł:"+new_issue.title+Environment.NewLine+"Opis:"+new_issue.description+Environment.NewLine+"Assignee:"+new_issue.assignee.name);
return new_issue;
}
}
对于新项目,有如下cURL:
curl --request POST --header "PRIVATE-TOKEN: <your-token>" \
--header "Content-Type: application/json" --data '{
"name": "new_project", "description": "New Project", "path": "new_project",
"namespace_id": "42", "initialize_with_readme": "true"}' \
--url 'https://gitlab.example.com/api/v4/projects/'
如何使用HttpRequestMessage??
4条答案
按热度按时间mwyxok5s1#
我认为你缺少的是POST主体,你可以在创建头文件后添加这些行:
w6lpcovy2#
看起来类似于this issue,也有看this one
另外,注意你的方法和变量名。它们看起来像python的名字和变量。
你的方法应该是
NewIssueAsync
而不是new_issue
,(例如)变量应该是string issuePath
而不是string issue_path
。vaj7vani3#
如果您无法使用
JsonContent
,因为您使用的是早期版本的。NET比.NET 5,你可以像这样回退到StringContent
:cgyqldqp4#
这样的东西可能对你有用: