I have always used RestSharp - but this project requires HttpClient. How would I add basic auth to this?
var json = JsonConvert.SerializeObject(jsonPost);
var data = new StringContent(json, Encoding.UTF8, "application/json");
var url = "";
using var client = new HttpClient();
var response = await client.PostAsync(url, data);
var result = await response.Content.ReadAsStringAsync();
Console.ReadLine();
CodePudding user response:
Try below implementation -
using var client = new HttpClient();
var byteArray = Encoding.ASCII.GetBytes("username:password");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var response = await client.PostAsync(url, data);
var result = await response.Content.ReadAsStringAsync();
CodePudding user response:
Add an Authorization header.
Authorization: Basic <"user:pass" encoded to base64>