0

Content being returned by HttpMessageResponse is blank/empty after being explicitly set

I have created a WebApi with the latest .net Framework 4.6.1 and am trying to pass back some TwiML back to Twilio (not really the issue/problem as I have the problem even with plain text).

For some reason, no matter what I do, when setting the Content for a HttpResponseMessage that content is not passed back to the caller. I have used Postman and Fiddler, and while the content header is showing after setting the content via StringContent, the actual content I am trying to pass back does not. It does however seem to be showing in the ContentLength.

I am really confused and may be overlooking something very simple, but over a day has been wasted so far.

I am showing just text trying to be sent back with nothing fancy and I get nothing back! In my real use case I am trying to pass back a string of XML as a type of text/xml. But even a basic text string does not come back!!

Any help is greatly appreciated, this has totally stumped me.

public HttpResponseMessage Post(TwilioCall call)
    {
        ...
        ...
        ...
        var responseMessage = new HttpResponseMessage
            {
                Content = new StringContent("Representative Text Here")
            };
        return responseMessage;
    }

The following is returned back to Postman, you can see the actual content is blank:

{
    "Version": {
        "Major": 1,
        "Minor": 1,
        "Build": -1,
        "Revision": -1,
        "MajorRevision": -1,
        "MinorRevision": -1
    },
    "Content": {
        "Headers": [
        {
            "Key": "Content-Type",
            "Value": ["text/plain; charset=utf-8"]
        }]
    },
    "StatusCode": 200,
    "ReasonPhrase": "OK",
    "Headers": [],
    "RequestMessage": null,
    "IsSuccessStatusCode": true
}

That was the pretty version. Here is what the response looks like raw. {"Version":"Major":1,"Minor":1,"Build":-1,"Revision":-1,"MajorRevision":-1,"MinorRevision":-1},"Content":{"Headers":[{"Key":"Content-Type","Value":["text/xml; charset=utf-8"]}]},"StatusCode":200,"ReasonPhrase":"OK","Headers":[],"RequestMessage":null,"IsSuccessStatusCode":true}

Another example of just a very plain vanilla post response:

:(

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.Net.Http;
using System.Text;
using MyNamespace.Models;

namespace MyNamespace.Controllers
{
[Route("[controller]")]
public class BasicTextResponseController : Controller
{
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // POST api/values
    [HttpPost]
    public HttpResponseMessage Post(TwilioCall call)
    {
        var responseMessage = new HttpResponseMessage
        {
            Content = new StringContent("Text to return.")
        };
        return responseMessage;
    }
}
}
drewid
  • 2,737
  • 3
  • 16
  • 11
  • Why does your postman's response look like this? Shouldn't it just plain text? Tried with my postman with the same code, it's working. I think somewhere in your code inject the response and change the output format, hence the incorrect response. – Cheng Chen Jun 17 '16 at 02:09
  • I don't know if it is a bug in the new framework or not? I am trying to output a really vanilla response. Will try more but I am crying right now. – drewid Jun 17 '16 at 03:40
  • Yeah, I created a very vanilla controller - going to add it to the end. All it does is accept a class on the post, nothing fancy. – drewid Jun 17 '16 at 03:50
  • What is the HTTP response code you're getting? – Marco Scabbiolo Jun 17 '16 at 05:09
  • 200 - it returns fine, just has no actual content, no matter what string, xml, etc. I put in the content. :( – drewid Jun 17 '16 at 05:14
  • Try the initialization of both HttpResponseMessage and StringContent shown here http://stackoverflow.com/questions/11581697/is-there-a-way-to-force-asp-net-web-api-to-return-plain-text – Marco Scabbiolo Jun 17 '16 at 05:19
  • @March - almost, but not go; still getting a blank. Maybe there is a bug in the framework. :( Ugghh - everything I find seems to be just too old as things mention Request.CreateResponse or Response.ContentType, etc. but that is no longer valid in the current framework. – drewid Jun 17 '16 at 05:31

1 Answers1

0

Instead of creating the HttpResponseMessage type directly, try:

var response = Request.CreateResponse<String>(HttpStatusCode.Created, "OK");
return response;

Also, if you are doing some sort of post, you can set the response headers to the location of the new item (like a PRG - post, redirect, get pattern):

response.Headers.Location = new Uri(...);

then return the response.

See if that works.

---------EDIT------------

You might have some other issues? Did you create your Controller from scratch? You should inherit from ApiController (and NOT Controller) for starters. The ApiController has different plumbing over the standard MVC Controller.

As for verification of the missing Request.CreateResponse() method, I don't know what version of studio you're using, but I went ahead and ran a repair on framework 4.6.1 just to assert that I have a good install. Since I'm using VS 2012, I also went ahead and downloaded framework 4.6 targeting pack. This allows me to select framework 4.6.1 when I created a local test webapi project.

Here's one of the method signatures for CreateResponse():

// Summary:
        //     Creates an System.Net.Http.HttpResponseMessage wired up to the associated
        //     System.Net.Http.HttpRequestMessage.
        //
        // Parameters:
        //   request:
        //     The HTTP request message which led to this response message.
        //
        //   statusCode:
        //     The HTTP response status code.
        //
        //   value:
        //     The content of the HTTP response message.
        //
        // Type parameters:
        //   T:
        //     The type of the HTTP response message.
        //
        // Returns:
        //     An initialized System.Net.Http.HttpResponseMessage wired up to the associated
        //     System.Net.Http.HttpRequestMessage.
        public static HttpResponseMessage CreateResponse<T>(this HttpRequestMessage request, HttpStatusCode statusCode, T value);

The using namespace is System.Net.Http; but this shouldn't be confused with the System.Net.Http.dll as these extensions reside in System.Web.Http.dll.

I hope that help. Perhaps I might have a few points returned back to me for the follow up.

alphaneer
  • 184
  • 3
  • Unfortunately Request.CreateResponse no longer exists. Not sure what version of the framework it changed but no longer available in 4.6.x for sure. – drewid Jun 19 '16 at 02:29