Return raw json string

I have a legacy system I’m trying to proxy calls to which returns Newtonsoft JObjects to the client.

What is the best way to return the deserialized JObject string from the endpoint maintaining it as json?

I’ve tried an HttpResult passing the json string and setting the json contenttype but it’s stringifying the response.

i.e. "{\"jsonProp\":\"jsonValue\"}

v.s, existing response

{ "jsonProp":"jsonValue"}

Do I need to write directly to the Response.Stream instead perhaps?

Your Services can return raw Data Types and here are some different ways HTTP responses can be customized.

So easiest way to return raw JSON is to set the ContentType with the [AddHeader] attribute and return the serialized raw JSON string.

[AddHeader(ContentType = MimeTypes.Json)]
public object Any(MyRequest request)
{
    return rawJsonString;
}

You can also return raw UTF-8 byte[] or Stream or if you prefer, you can write directly to the Response.OutputStream and end the request with Response.EndRequest().

1 Like

Thanks @mythz.

Went with just returning byte[] with the HttpResult and it works how I need it :+1:

1 Like