1
votes

I am trying to create a POST Request for Azure DevOps Teams and wish to create a new team through the API method. I have read the documentation on this, but I'm quiet new to this and don't have an idea how to properly implement this in my own project.

I get an error when running it. The error is due to return value. As I have very little experience using REST API's so would appreciate if someone could guide me into the right direction.

I am using Visual Studio with .NET Core 3.0.

Here is the code so far:

using System.IO;
using System;
using Microsoft.Azure.WebJobs;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs.Extensions.Http;

namespace TeamsAdd
{
    public static class TeamsAdd
    {
        [FunctionName("TeamsAdd")]
        public static HttpResponseMessage Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]
            HttpRequestMessage req)
        {
            var PAT = "xxxx";
            var body = new
            {
                name = "sampleTeamName",
                id = "xxxx",
                projectId = "xxxx",                 
            };

            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}", "", PAT))));

                //Connecting to the DevOps REST API
                var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"https://dev.azure.com/{organization}/_apis/projects/{projectId}/teams?api-version=6.0");
                requestMessage.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");

                //Reading Server Response
                using (HttpResponseMessage response = client.SendAsync(requestMessage).Result)
                {
                    response.EnsureSuccessStatusCode();
                }
            }
        }       
    }
}
1
Hi @merum Please share the error with us. Also I don't see $"https://dev.azure.com/{organization}/_apis/projects/{projectId}/teams?api-version=6.0" organization and projectId variables from this string substitution. Just to be sure, do you have them in your original code? - Krzysztof Madej
Yes I have those variables in my original code..plus the error is that Run() should return something which it is not. - maryerm
The error is CS0161 - 'TeamAdd.Run(HttpRequestMessage)': not all code paths return a value. - maryerm
Simply you are missing return statement. Please add return response after ` response.EnsureSuccessStatusCode();` - Krzysztof Madej
You probably shouldn't be calling the SendAsync() method within a Static class/method. Adding the .Result will force it run, but it's a bit flaky as to whether it will actually work. If you're making calls with the HttpClient it's usually safest to use the async/await pattern so that your method signature would be public asyc Task<HttpResponseMessage> Run. Also it's not clear what the HttpRequestMessage that you've passed in is for...? - Ross Halliday

1 Answers

0
votes

Regarding the issue, you can refer to the following code

[FunctionName("Function2")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req)
        {
            var PAT = "";
            var body = new
            {
                name = "mytest7878or",

            };

            using (HttpClient client = new HttpClient())
            {

                client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

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

                //Connecting to the DevOps REST API
                var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"https://dev.azure.com/{}/_apis/projects/{}/teams?api-version=6.0");
                requestMessage.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");

                //Reading Server Response
                using (HttpResponseMessage response = await client.SendAsync(requestMessage))
                {
                    if (!response.IsSuccessStatusCode)
                    {
                        
                        response.EnsureSuccessStatusCode();
                    }

                    return req.CreateResponse(HttpStatusCode.Created,"create successfully");
                }
            }
        }

enter image description here