Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Json data comes with backslash and not able to set accept media type

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.
api result output

If any one can help me here would great.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading