我想发一个POST请求来创建发布api,示例:
发布https://fabrikam.vsrm.visualstudio.com/MyFirstProject/_apis/release/releases?api-version=4.1-preview.6
使用以下请求正文:
{
"definitionId": 1,
"description": "Creating Sample release",
"artifacts": [
{
"alias": "Fabrikam.CI",
"instanceReference": {
"id": "2",
"name": null
}
}
],
"isDraft": false,
"reason": "none",
"manualEnvironments": null
}
任何人都可以给我一些指示,在C#中进行相同的编码。我此刻迷失了。
亲切的问候。
EDIT1: 我有以下代码来获取响应,我想对它进行POST:
class Program
{
public async void GetProjects()
{
try
{
var personalaccesstoken = "PAT";
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 = client.GetAsync(
"https://sample.vsrm.visualstudio.com/MyFirstProject/_apis/release/releases?api-version=4.1-preview.6").Result)
//POST to response with Json body
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
static void Main(string[] args)
{
Program prog = new Program();
prog.GetProjects();
}
}
答案 0 :(得分:0)
好的,我可以在RestSharp的帮助下完成这项工作:
var client = new RestClient("https://demouser.vsrm.visualstudio.com/MyFirstProject/_apis/release/releases?api-version=4.1-preview.6");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Basic TWFuaXNoX0JpbHVuZ0BhZC5pbmZvc3lzLmNvbTpqbXZzNzQzeTZzcjR2cWFjY3Y0ZnpmbXk1ZTNra2tiYm03aDJjb29wMmZ5bjZ2MjZ1NW9x");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("undefined", "{\r\n \"definitionId\": 1,\r\n \"description\": \"Creating Sample release\",\r\n \"artifacts\": [\r\n {\r\n \"alias\": \"_MyFirstProject\",\r\n \"instanceReference\": {\r\n \"id\": \"2\",\r\n \"name\": null\r\n }\r\n }\r\n ],\r\n \"isDraft\": false,\r\n \"reason\": \"none\",\r\n \"manualEnvironments\": \"PROD\"\r\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);