{"id":1018,"date":"2023-07-28T10:36:40","date_gmt":"2023-07-28T09:36:40","guid":{"rendered":"https:\/\/wp.bizoir.dk\/?p=1018"},"modified":"2025-02-16T10:04:51","modified_gmt":"2025-02-16T09:04:51","slug":"returning-json-response-from-azure-function-v3","status":"publish","type":"post","link":"https:\/\/wp.bizoir.dk\/?p=1018","title":{"rendered":"Returning Json response from Azure Function v3+"},"content":{"rendered":"<p>It can be a pain to return json-responses from a Function App, when you also want the proper Content-Type HTTP header to be returned, which it of course should.<\/p>\n<p>If you do it the straight-forward way (works the same with OkObjectResult) &#8211;<\/p>\n<pre>return new BadRequestObjectResult(jsonResponse);<\/pre>\n<p>if response contains a json string, it will be returned ok, but the Content-Type will be &#8220;text\/plain&#8221;<\/p>\n<pre>BadRequestObjectResult resp = new BadRequestObjectResult(jsonResponse);\r\nresp.ContentTypes.Add(MediaTypeHeaderValue.Parse(\"application\/json\"));\r\nresp.Value = jsonResponse;\r\nreturn resp;<\/pre>\n<p>if response contains a json string, the special characters (like &#8220;) will be escaped, and it will no longer return valid json, but the Content-Type is ok (application\/json)<\/p>\n<p>If the content-type is not set in above example, the special characters are not escaped<\/p>\n<p><!--more--><\/p>\n<p>If you return a JsonResult as suggested in some places, the ContentType will be correct, but the Json will be escaped.<\/p>\n<pre>return new JsonResult(jsonResponse);<\/pre>\n<p>Make a class like this<\/p>\n<pre>using System.Text.Json;\r\nusing Microsoft.AspNetCore.Mvc;\r\n\r\nnamespace Azure.Function.Returning.Json.Response {\r\n  public class CustomJsonResult : ContentResult {\r\n    private const string ContentTypeApplicationJson = \"application\/json\";\r\n\r\n    public CustomJsonResult(object p_value, JsonSerializerOptions p_options = null, int p_statusCode = 200) {\r\n      ContentType = ContentTypeApplicationJson;\r\n      Content = p_options == null ? JsonSerializer.Serialize(p_value) : JsonSerializer.Serialize(p_value, p_options);\r\n      StatusCode = p_statusCode;\r\n    }\r\n  }\r\n}<\/pre>\n<p>and return response like<\/p>\n<pre> return new CustomJsonResult(jsonResponse, new JsonSerializerOptions { PropertyNamingPolicy = null }, 400);<\/pre>\n<p>This way, correct json will be returned as well as correct Content-Type<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It can be a pain to return json-responses from a Function App, when you also want the proper Content-Type HTTP header to be returned, which it of course should. If you do it the straight-forward way (works the same with OkObjectResult) &#8211; return new BadRequestObjectResult(jsonResponse); if response contains a json string, it will be returned &hellip; <a href=\"https:\/\/wp.bizoir.dk\/?p=1018\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Returning Json response from Azure Function v3+&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,1],"tags":[],"class_list":["post-1018","post","type-post","status-publish","format-standard","hentry","category-guides","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/posts\/1018","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1018"}],"version-history":[{"count":4,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/posts\/1018\/revisions"}],"predecessor-version":[{"id":1121,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/posts\/1018\/revisions\/1121"}],"wp:attachment":[{"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}