0

For new issue GitLab Api has a cURL example:

curl --request POST --header "PRIVATE-TOKEN: <your_access_token>"
"https://gitlab.example.com/api/v4/projects/4/issues?title=Issues%20with%20auth&labels=bug"

I'm using HttpRequestMessage method like this:

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;
    }            
}

For new projects there is cURL like this:

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/'

How to do it with HttpRequestMessage??

DasKrümelmonster
  • 5,816
  • 1
  • 24
  • 45
simoN
  • 65
  • 2
  • 12
  • any indication? – simoN Dec 23 '22 at 22:06
  • Not sure if its a problem, but in your issue_path you are doing ... + "/issues/" + ... which is wrong. According to the curl it should be ...+ "/issues" + ... – cmos Jan 05 '23 at 10:03

4 Answers4

2

I think what you're missing is the POST body, you can add these lines after you create your header:

    var body = new
    {
        name = "new_project",
        description = "New Project",
        path = "new_project",
        namespace_id = "42",
        initialize_with_readme = "true"
    };
    request.Content = JsonContent.Create(body);
Costa
  • 1,794
  • 1
  • 12
  • 21
0

Looks similar to this issue, also have a look to this one

Also, pay attention to your methods and variable names. They look like python's names and variables.

Your should have NewIssueAsync instead of new_issue for the method, and (for example) string issuePath instead of string issue_path for your variables.

Scryper
  • 178
  • 1
  • 14
0

If you can't use JsonContent because you are using earlier version of .NET than .NET 5 you can fall back to StringContent like this:

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;
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
0

Something like this may work for you:

    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;
    }
Mukul Keshari
  • 495
  • 2
  • 7