Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting content-type to application/json

I have this C# code in my Azure function:

var data = new StringContent(requestBody, Encoding.UTF8, "application/json");
var request = await GetSubmissions.PostAsync("url", data);

if (request.IsSuccessStatusCode)
{
    content = await request.Content.ReadAsStringAsync();
    var response = req.CreateResponse(request.StatusCode);
    await response.WriteStringAsync(content);
    return response;
}

This receives data from an API call, and then returns it as response from that Azure function. All is good and I can see it being returned in Postman just fine, but I need to change the content type to JSON within the response as it is coming in as text.

As shown in this screenshot:

enter image description here

What can I try next?

like image 844
Jason_Hough Avatar asked Mar 09 '26 03:03

Jason_Hough


1 Answers

Try explicitly setting the response header:

response.Headers.Add("Content-Type", "application/json; charset=utf-8");

Another option (though less preferable one) would be to deserialize the response and then serialize it back with WriteAsJsonAsync.

like image 71
Guru Stron Avatar answered Mar 11 '26 17:03

Guru Stron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!