Hi I am facing issue in setting up (Accept : "application/vnd.api+json") in header and my json result returns backslash (picture attached)
[Route("api/[controller]")]
[ApiController]
[Produces("application/json")]
public class project : ControllerBase
{
private readonly IConfiguration _configuration;
private readonly HttpClient _httpClient;
public project(IConfiguration configuration, HttpClient httpClient)
{
_configuration = configuration;
_httpClient = httpClient;
}
[HttpGet]
public async Task<IActionResult> Get()
{
try
{
// Set the API key in the request headers
_httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", _configuration["ApiSettings:ApiKey"]);
var content = new StringContent("", Encoding.UTF8, "application/json");
var authResponse = await _httpClient.PostAsync("https://www.example.com/auth", content);
if (!authResponse.IsSuccessStatusCode)
{
return BadRequest("Not found");
}
// Parse the initial response content
var authToken = await authResponse.Content.ReadAsStringAsync();
_httpClient.DefaultRequestHeaders.Accept.Clear();
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", authToken);
var url = "https://www.example.com/api/credential/";
var personCredentials = await _httpClient.GetAsync(url);
return Ok(await personCredentials.Content.ReadAsStringAsync());
} catch (HttpRequestException ex)
{
string errorMessage = $"Error making API call: {ex.Message}";
return StatusCode((int)ex.StatusCode, errorMessage);
}
}
}
I am using jsonplaceholder api to show the result output. The api I will be using header with token and (Accept : "application/vnd.api+json"). I tried adding this but it gave me media not support error.

If any one can help me here would great.
>Solution :
Your result is a double encoded json string, this happens because you read the data as string and return it as json (see the ControllerBase.Ok method signature, it accepts object and will result in serialization of the value). You need either deserialize the string and pass it to Ok or try to return content result directly. For example:
return Content(await personCredentials.Content.ReadAsStringAsync());