1
votes

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.

2
The format is correct. Looks like url really "not found" :)antonio_antuan
@АнтониоАнтуан 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
try this: getpostman.com. It allows you to make custom requests, e.g. POST. Also, Chrome plugin is availableantonio_antuan
@АнтониоАнтуан You are right. I will edit my question because it is certainly not about Python at all.ozgur
How you are posting, plz add that code/approach also.Anil

2 Answers

1
votes

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)
0
votes

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);