简体   繁体   中英

Azure DevOps REST API

I have read the Azure DevOPS REST API documentation and tried to implement it to my Web Application multiple times but to no avail. I have no experience using REST API's and I would appreciate if someone could guide me into the right direction.

I am trying to create a POST Request for Azure DevOps Repositories and wish to create a new repository through the API method. I have read the documentation on this, but I have no idea how to implement this in my own project. I understand how I need to create a connection to the API, but no idea how and where I write the Request Body for this method. I would like to know how I specify the name of the new repository. I'm actually very clueless and have no idea how to use the REST API in general.

I am using Visual Studio with .NET Core 3.0 and plan to use this with React.js

Here's the code I'm working with so far, and I have no idea where to go from here:

public class AzureDevOps { 
    public static async void GetRepositories()
    {
        try
        {
            var personalaccesstoken = "PAT_FROM_WEBSITE";

            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(
                    new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                    Convert.ToBase64String(
                        System.Text.ASCIIEncoding.ASCII.GetBytes(
                            string.Format("{0}:{1}", "", personalaccesstoken))));

                using (HttpResponseMessage response = await client.GetAsync(
                            "https://dev.azure.com/{organization}/_apis/git/repositories?api-version=5.1"))
                {
                    response.EnsureSuccessStatusCode();
                    string responseBody = await response.Content.ReadAsStringAsync();
                    Console.WriteLine(responseBody);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}

I would appreciate any clarification on this matter, as well as some examples on how to use the REST API. Thanks in advance!

You should use POST method to create a repository. Check the API here:

https://docs.microsoft.com/en-us/rest/api/azure/devops/git/repositories/create?view=azure-devops-rest-5.1

The code should look like:

                var PAT = "xxxxx";

                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                        Convert.ToBase64String(
                            System.Text.ASCIIEncoding.ASCII.GetBytes(
                                string.Format("{0}:{1}", "", PAT))));
                    var requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=5.1");
                    requestMessage.Content = new StringContent("{\"name\": \"RepositoryName\",\"project\": {\"id\": \"xxxxxxx\"}}", Encoding.UTF8, "application/json");
                    using (HttpResponseMessage response = client.SendAsync(requestMessage).Result)
                    {
                        response.EnsureSuccessStatusCode();
                    }
                }

Update:

       var PAT = "xxxxx";
       var body = new
            {
                name = "RepositoryName",
                project = new
                {
                    id = "xxxxxxx"
                }
            };

                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                        Convert.ToBase64String(
                            System.Text.ASCIIEncoding.ASCII.GetBytes(
                                string.Format("{0}:{1}", "", PAT))));
                    var requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=5.1");
                    requestMessage.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
                    using (HttpResponseMessage response = client.SendAsync(requestMessage).Result)
                    {
                        response.EnsureSuccessStatusCode();
                    }
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM