XYZ
XYZ

Reputation: 317

Send JSON response from middleware

here's the code:

public Task Invoke(HttpContext httpContext)
        {
            if (Http_Server.mode == 2)
                return InteractivityTask(httpContext);
            else
                return _next(httpContext);
        }

public async Task<HttpResponse> InteractivityTask(HttpContext httpContext) {

            HttpResponse response = httpContext.Response;
            string s = getResponseBody();
            response.Clear();
            MemoryStream ms = GenerateStreamFromString(s);
            response.Body = ms;
            response.ContentType = "application/json";//not sure
            }
            return response;
        }

public MemoryStream GenerateStreamFromString(string value)
        {
            return new MemoryStream(Encoding.UTF8.GetBytes(value ?? ""));
        }

I am not receiving the body of the response, just the status code with blank body received.

Upvotes: 2

Views: 1120

Answers (1)

user1179299
user1179299

Reputation: 366

Try this:

response.Body.Write(ms.ToArray(), 0, (int)ms.Length);

Instead of:

response.Body = ms;

Upvotes: 1

Related Questions