Sujeet Raman

Sujeet Raman

  • 864
  • 927
  • 352.9k

Http context Request Header=false in httpclient post method

Feb 11 2021 11:11 AM
Hi,
I was trying to communicate 2 web api solution using httpclient.I put breakpoint on 2nd solution post method.
I found in my request header in my API 2 HasConnection = false
 
in
 
var checkApiKeyExists = context.HttpContext.Request.Headers.TryGetValue("x-api-key", out var lsHeaders);
 
full method
 
public override void OnActionExecuting(ActionExecutingContext context)
{
bool isValidApiKey = false;
//Validate that the api key exists
var checkApiKeyExists = context.HttpContext.Request.Headers.TryGetValue("x-api-key", out var lsHeaders);
if (lsHeaders.Count > 0 && checkApiKeyExists && lsHeaders.First().Equals(_apiKey, StringComparison.InvariantCulture))
{
isValidApiKey = true;
}
 
here context.HttpContext.Request.Headers headers has connection is false and the var api becomes false so i am not able to get the
response.How can i make has connection true in my request
 
 
API 1 sending request to API 2
public async Task CallemployeeService(Resultclass obj)
{
var requestUri = string.Format(url.employeeServiceCall);
var requestData = obj;
// Serialize into a JSON String
var stringPayload = await Task.Run(() => JsonConvert.SerializeObject(requestData));
// Wrapping JSON inside a StringContent which then can be passed to HttpClient class
var httpContent = new System.Net.Http.StringContent(stringPayload, System.Text.Encoding.UTF8, "application/json");
var httpResponse = await _singleton.Client.PostAsync(requestUri, httpContent);
switch (httpResponse.StatusCode)
{
case HttpStatusCode.OK:
var jsonResponse = await httpResponse.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject(jsonResponse);
return result;
}