RestPostAsync

Parent Previous Next

RestPostAsync

Description: the method executes a Post Rest request with sending data in json format and receives the result in the same way in json format

Syntax: RestPostAsync(string url, string json, TimeSpan timeout, string charset = "UTF-8")

Return value: string


Example:

string result = await tester.RestPostAsync("https://jsonplaceholder.typicode.com/posts/1/", "{}", TimeSpan.FromDays(1), "UTF-8");

tester.ConsoleMsg(result);


This method uses a standard approach:

using System.Net;

using System.Net.Http;

using System.Net.Http.Headers;


Uri uri = new Uri(url);

HttpClient client = new HttpClient();

client.Timeout = TimeSpan.FromDays(1);

client.DefaultRequestHeaders.Add("charset", "UTF-8");

client.DefaultRequestHeaders.Add("User-Agent", userAgent);

HttpContent content = new StringContent("{}", Encoding.UTF8, "application/json");

HttpResponseMessage response = await client.PostAsync(uri, content);

if (response.IsSuccessStatusCode)

{

       return await response.Content.ReadAsStringAsync();

}

Created with the Personal Edition of HelpNDoc: Full-featured EPub generator