0

I have a C# WebAPI action. I'm trying to post a string (but a json stringified string) to it using React.

For some reason I'm getting a 405 Method Not Allowed when the content type is aopplication/json. Urlencoded does get into PostTransaction, but body is always null. What do I need to change to be able to get json in PostTransaction?

WebConfig

<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

WebApiConfig

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));

C# WebApi

[HttpPost]
[ActionName("PostTransaction")]
public string PostTransaction([FromBody] string body, string task, int key, string filters, string options)
{
    // body is null from react when content type is urlencoded, but correct from jquery post
    string returnString = "no data found";
    if (body != null) {
        returnString = body.ToString();
    }

    return returnString;
}

React

var dataToSend = JSON.stringify({ param1: "value1", param2: "value2"});
fetch('http://localhosturl/PostTransaction',
  {
    method: 'post',
    headers: {
      'Accept': 'application/json, text/plain, */*',
      'Content-Type': 'application/json'
    },
    body: dataToSend
  })
  .then(response => {
}

JQuery Post

var dataToSend = JSON.stringify({ param1: "value1", param2: "value2"});
$.post('http://localhosturl/PostTransaction', { '': dataToSend }).done(function (data) {
     console.debug(data); // returned data from Web API
});

Thanks

Richard Whitehouse
  • 679
  • 3
  • 14
  • 28
  • 2
    Why exactly are you using `'Content-Type': 'application/x-www-form-urlencoded'`? You are sending JSON not a form – Camilo Terevinto Sep 09 '18 at 21:44
  • 1
    Do you mean to do JSON.stringify twice in your react code? – Alexander Staroselsky Sep 09 '18 at 21:44
  • 1
    https://stackoverflow.com/questions/38510640/how-to-make-a-rest-post-call-from-reactjs-code – Gauravsa Sep 09 '18 at 22:16
  • The reason I'm using urlencoded is because I get a 405 error if using application/json. Maybe that's my main problem. Are these supported mediatypes not enough? config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json")); – Richard Whitehouse Sep 09 '18 at 22:22
  • 1
    never hack around those problem..."hmmm, can't send application/json, oh well, lets send it urlencoded".... is never a good idea and makes it even more confusing when you come to SO :) – Keith Nicholas Sep 09 '18 at 22:30
  • Fair comment @KeithNicholas. I've probably been stuck on this problem for so long and read so many posts, I was a bit lost where the problem actually was. urlencoded was getting deeper than json, so I'd got stuck on that one. – Richard Whitehouse Sep 09 '18 at 22:42
  • You don't seem to have included a controller in your url. Is that because of the edit for SO? – ProgrammingLlama Sep 09 '18 at 23:49
  • @John Yes it's a bit of an SO edit, but C# WebApi is in my controller, and basically the only thing that should be hit inside my controller. The only thing that my code hits that isn't documented is the routing above the media types in WebAPIConfig. – Richard Whitehouse Sep 10 '18 at 18:38

1 Answers1

1

So the reason for the difference, is that I needed to convert the object in React, to a string, then stringify it again.

My original post did do this, but at the same time I changed it from urlencoded to application/json, if I'd just originally changed it (see history) to application/json without removing the second stringify, it would have worked ok.

body: JSON.stringify(JSON.stringify(dataToSave))

This is so that when it's actually posted, it still has the escaped quotes.

Richard Whitehouse
  • 679
  • 3
  • 14
  • 28