1

I am creating an api conroller class in VB. I have a very simple function in it:

Public Function Post(<FromBody()> ByVal value As String) As String
    Return value
End Function

When I send a POST request from HTTP Tool (FireFox extension), I can see it go in the function, but value is always empty.

I have this in my WebApiConfig.vb:

config.Routes.MapHttpRoute(
        name:="Names",
        routeTemplate:="{controller}/{id}",
        defaults:=New With {.id = RouteParameter.Optional}

And this in Global.ASAX.vb under Application-Start():

RouteTable.Routes.MapHttpRoute(name:="Post", routeTemplate:="post", defaults:=New With {.symbol = RouteParameter.Optional, .controller = "Names"})

I tried this from Fiddler 4 as well, but I get:

{"Message":"The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.","ExceptionMessage":"No MediaTypeFormatter is available to read an object of type 'String' from content with media type 'application/octet-stream'.","ExceptionType":"System.Net.Http.UnsupportedMediaTypeException","StackTrace":" at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable '1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable '1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"}

Or when I try to set the content-type in the header I get:

No MediaTypeFormatter is available to read an object of type 'String' from content with media type ...

Where <...> is whatever media type I set.

How do I make this simple POST work?

Jeroen
  • 460
  • 6
  • 14

1 Answers1

0

It looks like the framework you are using for your API doesn't know how to parse the content of the body of your POST request based on the content type header that you are setting in your request.

Also, your API controller may not know how to parse plain text.

You can either manually add a supported content type, or read in the request content manually according to one of the answers in the above link. Or you can change your controller to accept a form post like so.

Community
  • 1
  • 1
arjabbar
  • 6,044
  • 4
  • 30
  • 46
  • This is probably a fairly decent answer to the question. However, I have gone a different route and can't really justify the time re-testing this with the answer provided. Hopefully someone else finds this useful. – Jeroen Mar 30 '17 at 02:43