将cURL语法重写为HttpRequest

plicqrtu  于 2023-04-30  发布在  其他
关注(0)|答案(4)|浏览(149)

新一期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??

mwyxok5s

mwyxok5s1#

我认为你缺少的是POST主体,你可以在创建头文件后添加这些行:

var body = new
    {
        name = "new_project",
        description = "New Project",
        path = "new_project",
        namespace_id = "42",
        initialize_with_readme = "true"
    };
    request.Content = JsonContent.Create(body);
w6lpcovy

w6lpcovy2#

看起来类似于this issue,也有看this one
另外,注意你的方法和变量名。它们看起来像python的名字和变量。
你的方法应该是NewIssueAsync而不是new_issue,(例如)变量应该是string issuePath而不是string issue_path

vaj7vani

vaj7vani3#

如果您无法使用JsonContent,因为您使用的是早期版本的。NET比.NET 5,你可以像这样回退到StringContent

var payload = new
{
     name = "new_project",
     description = "New Project",
     path = "new_project",
     namespace_id = "42",
     initialize_with_readme = "true"
};
var stringPayload = JsonConvert.SerializeObject(payload);

var content = new StringContent(stringPayload, Encoding.UTF8, "application/json");
request.Content = content;
cgyqldqp

cgyqldqp4#

这样的东西可能对你有用:

static async Task<Project> NewProject(string repository,
        string projectName,
        string projectDescription,
        string projectPath,
        int namespaceId,
        bool initializeWithReadme) 
    {
        string projectsPath = which_repo(repository)[0] + "projects/";
        using var request = new HttpRequestMessage(new HttpMethod("POST"), projectsPath);

        request.Headers.TryAddWithoutValidation("PRIVATE-TOKEN", which_repo(repository)[1]);
        var payload = new
        {
            name = projectName,
            description = projectDescription,
            path = projectPath,
            namespace_id = namespaceId,
            initialize_with_readme = initializeWithReadme
        };
        var stringPayload = JsonConvert.SerializeObject(payload);
        var body = new StringContent(stringPayload, Encoding.UTF8, "application/json");
        request.Content = body;

        var response = await client.SendAsync(request);
        var content = await response.Content.ReadAsStringAsync();

        Project newProject = JsonConvert.DeserializeObject<Project>(content);
        Console.WriteLine($"Utworzono Project:{Environment.NewLine}" + content);
        return newProject;
    }

相关问题