Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading FromUri and FromBody at the same time

I have a new method in web api

[HttpPost] public ApiResponse PushMessage( [FromUri] string x, [FromUri] string y, [FromBody] Request Request) 

where request class is like

public class Request {     public string Message { get; set; }     public bool TestingMode { get; set; } } 

I'm making a query to localhost/Pusher/PushMessage?x=foo&y=bar with PostBody:

{ Message: "foobar" , TestingMode:true } 

Am i missing something?

like image 381
kkocabiyik Avatar asked Aug 22 '12 11:08

kkocabiyik


People also ask

What is the difference between FromBody and FromUri?

The [FromUri] attribute is prefixed to the parameter to specify that the value should be read from the URI of the request, and the [FromBody] attribute is used to specify that the value should be read from the body of the request.

Can we use FromBody with Httpget?

Please note that we are able to send [FromBody] parameter in HTTP GET Request input.

What is the use of FromBody?

Using [FromBody] When a parameter has [FromBody], Web API uses the Content-Type header to select a formatter. In this example, the content type is "application/json" and the request body is a raw JSON string (not a JSON object).

What is FormBody?

To change the default parameter binding process use [FormBody] and [FormUri] attributes. Here, the id with an integer type is declared as [FormBody] attribute. Here in this example, WebAPI is going to look for the request in the value parameter and also in the body parameter and process the request accordingly.


1 Answers

A post body is typically a URI string like this:

Message=foobar&TestingMode=true 

You have to make sure that the HTTP header contains

Content-Type: application/x-www-form-urlencoded 

EDIT: Because it's still not working, I created a full example myself.
It prints the correct data.
I also used .NET 4.5 RC.

// server-side public class ValuesController : ApiController {     [HttpPost]     public string PushMessage([FromUri] string x, [FromUri] string y, [FromBody] Person p) {         return p.ToString();     } }  public class Person {     public string Name { get; set; }     public int Age { get; set; }      public override string ToString() {         return this.Name + ": " + this.Age;     } }  // client-side public class Program {     private static readonly string URL = "http://localhost:6299/api/values/PushMessage?x=asd&y=qwe";      public static void Main(string[] args) {         NameValueCollection data = new NameValueCollection();         data.Add("Name", "Johannes");         data.Add("Age", "24");          WebClient client = new WebClient();         client.UploadValuesCompleted += UploadValuesCompleted;         client.Headers["Content-Type"] = "application/x-www-form-urlencoded";         Task t = client.UploadValuesTaskAsync(new Uri(URL), "POST", data);         t.Wait();     }      private static void UploadValuesCompleted(object sender, UploadValuesCompletedEventArgs e) {         Console.WriteLine(Encoding.ASCII.GetString(e.Result));     } } 
like image 153
Johannes Egger Avatar answered Sep 17 '22 08:09

Johannes Egger