0

I want to POST data to WebAPI. Ideally I would just do:

http:www.myawesomesite.com?foo=bar

As a POST. But to my specific question, I am trying to use:

using(var webClient = new WebClient())
{
    client.uploadString("http:www.myawesomesite.com", "POST", "foo=bar");
}

But that converts "foo=bar" to a bye array. Okay fine, I'm just trying to get it to work at this point.

My Web API controller looks like this:

[HttpPost]
public void MashPotatos(string foo)
{
    potatoMasher.Mash(foo);
}

But I get The remote server returned an error: (404) Not Found. First off, I thought WebAPI would automatically grock that data for me even if it was in the body of the request. But more importantly, I just want to be able to get it to work.

Ideally, I'd like to leave the WebAPI method in a form such that you can still invoke it by using a query string with a POST verb.

user1873073
  • 3,580
  • 5
  • 46
  • 81

3 Answers3

1

you need to configure your web API route to accept a foo parameter.May be this will solve your issue

config.Routes.MapHttpRoute(name: "newont",
                routeTemplate: "api/{controller}/{foo}",
                defaults: new { controller = "Controllername", foo= "foo"}
            );
MMM
  • 3,132
  • 3
  • 20
  • 32
  • If I don't use a query string do I still need to use this? No, right? But it's still not working. Query string is ideal but I can skip it. – user1873073 Mar 24 '15 at 14:15
  • 404 error may be Some error will be in your urI https://msdn.microsoft.com/en-us/library/d0d3595k(v=vs.110).aspx from this method will work once you change the route config and check your uri – MMM Mar 24 '15 at 14:25
0

Well here is a post that might be helpful to some. For me I just had to do:

using(var webClient = new WebClient())
{
    client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    client.uploadString("http:www.myawesomesite.com", "POST", "foo=bar");
}

and

[HttpPost]
public void MashPotatos([FromBody]string foo)
{
    potatoMasher.Mash(foo);
}

I decided against doing a POST with a query string as it seems to go against convention but there is also a [FromUri] attribute

Community
  • 1
  • 1
user1873073
  • 3,580
  • 5
  • 46
  • 81
0
public string GetData(){
string jsonResponse = string.Empty;using (WebClient client = new WebClient()){client.Headers[HttpRequestHeader.ContentType] = "application/json";
  jsonResponse = client.UploadString(baseuri, "POST", @"{personId:""1"", startDate:""2018-05-21T14:32:00"",endDate:""2018-05-25T18:32:00""}");  
return JsonConvert.DeserializeObject<Model>(jsonResponse);}}

@"{personId:""1"", startDate:""2018-05-21T14:32:00"",endDate:""2018-05-25T18:32:00""}

It's a JSON custom string, or you can serialize the object here on the API side

HttpPost

    public string Metod(Something data)
    {
        DateTimeOffset sDate = DateTimeOffset.Parse(data.date1);
        DateTimeOffset eDate = DateTimeOffset.Parse(data.date2);
        return _someService.GetService(data.Id, sDate, eDate);
    }

Then we go to the service and get data from DB

Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
maria
  • 1