Skip to content

Instantly share code, notes, and snippets.

@6rube
Created February 3, 2022 10:33
Show Gist options
  • Save 6rube/6298932e7b1ab3a1b8d255ffeb3e55de to your computer and use it in GitHub Desktop.
Save 6rube/6298932e7b1ab3a1b8d255ffeb3e55de to your computer and use it in GitHub Desktop.
Async HTTP Post Request with Auth
using System.Net.Http.Headers;
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "TOKEN"); //Add Authentication (extra)
var data = @"{JSON}"; //JSON String
var buffer = System.Text.Encoding.UTF8.GetBytes(data);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); //Type Of Data
var response = await client.PostAsync("http://127.0.0.1/createSomething", byteContent); //URL
var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString); //out to terminal (extra)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment