1

I have a web api method which is as follows,

[Route("api/Nltk")]
[HttpPost]
public string Create([FromBody]string text)
{
    string result = "Error";
    //Assign result here
    return result;
}

When I make a POST request I get 404 - File or directory not found. error. While other methods (which are all GET methods) in the same api work just fine. For further detail http://ozgurakpinar.net/api/Nltk is the complete url.

The following is one of the methods I've tried, so far.

 var values = new Dictionary<string, string> {
    { "text", "This is a relatively short sentence." },
    };
    HttpClient client = new HttpClient();
    var content = new FormUrlEncodedContent(values);
    var result = client.PostAsync("http://ozgurakpinar.net/api/Nltk", content).Result;

Edit: After I added the FromBody attribute the method is finally called, but the value of text is null.

ozgur
  • 2,549
  • 4
  • 25
  • 40
  • 3
    The format is correct. Looks like url really "not found" :) – antonio_antuan Feb 01 '16 at 15:30
  • @АнтониоАнтуан But I just published it and other web api methods work just fine. But all the other ones are GET methods=) I am stuck with the post. – ozgur Feb 01 '16 at 15:38
  • 1
    try this: https://www.getpostman.com. It allows you to make custom requests, e.g. POST. Also, Chrome plugin is available – antonio_antuan Feb 01 '16 at 15:41
  • @АнтониоАнтуан You are right. I will edit my question because it is certainly not about Python at all. – ozgur Feb 01 '16 at 15:43
  • How you are posting, plz add that code/approach also. – Anil Feb 01 '16 at 15:49
  • @AnilKumar I added the details now. – ozgur Feb 01 '16 at 15:52
  • Have you done all required configuration to make Attribute Routing work , bascically config.MapHttpAttributeRoutes(); you can refer details here http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2 – Anil Feb 01 '16 at 16:18
  • Where is the value of `text` coming from, a form or the url? Your parameter needs to be bound. – Drew Kennedy Feb 01 '16 at 16:19
  • @AnilKumar I did it inside WebApiConfig.cs file. My other GET methods which also use attribute routing works. – ozgur Feb 01 '16 at 16:20
  • It's null because you need to use `FromUri` and not `FromBody`. – Drew Kennedy Feb 01 '16 at 16:24
  • @DrewKennedy When I change FormBody to FormUri the method is not called this time. Debug is not entered the method. – ozgur Feb 01 '16 at 16:27
  • How are you testing your url? Google's REST client? Try testing it as such: `http://ozgurakpinar.net/api/Nltk?text=mystring` – Drew Kennedy Feb 01 '16 at 16:29
  • @DrewKennedy After I got stuck with this problem, I am testing it locally. On my own computer, debugging it. Get methos work perfectly but post is such a pain in the ass. – ozgur Feb 01 '16 at 16:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102267/discussion-between-drew-kennedy-and-ozgur). – Drew Kennedy Feb 01 '16 at 16:35
  • Possible duplicate of [FromBody value get null](http://stackoverflow.com/questions/16837844/frombody-value-get-null) – Anil Feb 02 '16 at 05:53

2 Answers2

1

First, I think you may have a typo. It should be [FromBody] not [FormBody].

Secondly, you need to append an "=" before your content string.

ie:

client.PostAsync("http://ozgurakpinar.net/api/Nltk", "=" + content)
sonofaforester
  • 337
  • 1
  • 10
  • I was actually using full name System.Web.Http.FromBody and I just perceived it wrong. But it is correct in the code. My question is how can I concatenate "=" with a HttpContent type? content is not a string. – ozgur Feb 01 '16 at 16:41
0

When you are giving a name to your value then actually you are looking for a class with that member. In your case you are posting to a method which accept a class having a text member of string type.

If you need to post to a method having a string parameter then no need to give it a name. Below is working code.

var values = new Dictionary<string, string> {{ "", "This is a relatively short sentence." }};
            var content = new FormUrlEncodedContent(values);
            var client = new HttpClient();
            var result = client.PostAsync("http://localhost:49923/api/Nltk", content).Result;
            Console.Write(result);
Anil
  • 3,722
  • 2
  • 24
  • 49
  • This worked. But I still think POST is problematic in Web Api. A dictionary where **key** is an empty string? How come it can hash an empty string? – ozgur Feb 02 '16 at 12:10