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

$"https://dev.azure.com/{organization}/_apis/projects/{projectId}/teams?api-version=6.0"organizationandprojectIdvariables from this string substitution. Just to be sure, do you have them in your original code? - Krzysztof Madejreturn responseafter ` response.EnsureSuccessStatusCode();` - Krzysztof Madejpublic asyc Task<HttpResponseMessage> Run. Also it's not clear what theHttpRequestMessagethat you've passed in is for...? - Ross Halliday