C # HTTP Post Arguments

I am making an HTTP POST method to receive data. I have an idea to create a method to get specific arguments, but when I have no idea to get arguments. In HTTP GET, the arguments are in the URL and it is easier to get the arguments. How to create a method to transfer all data to HTTP Post? In PHP, for example, when you show var $ _POST, you show all the data in the body message. How can I do this in C #?

My method is this:

[HttpPost] [AllowAnonymous] public IHttpActionResult Test() { // Get URL Args for example is var args = Request.RequestUri.Query; // But if the arguments are in the body i don't have idea. } 
+6
source share
2 answers

The Web API has a function that automatically binds the argument sent to the action inside the controller. This is called parameter binding . It allows you to simply request an object inside the URL or body of the POST request, and it uses the deserialization magic for you using a thing called Formatters. There is a formatter for XML, JSON, and other well-known types of HTTP requests.

For example, let's say I have the following JSON:

 { "SenderName": "David" "SenderAge": 35 } 

I can create an object that matches my query, we will call it SenderDetails :

 public class SenderDetails { public string SenderName { get; set; } public int SenderAge { get; set; } } 

Now, having received this object as a parameter in my POST action, I tell WebAPI to try to bind this object for me. If all goes well, I will have the information available to me without parsing it:

 [Route("api/SenderDetails")] [HttpPost] public IHttpActionResult Test(SenderDetails senderDetails) { // Here, we will have those details available, // given that the deserialization succeeded. Debug.Writeline(senderDetails.SenderName); } 
+3
source

If I get you right, in C # you use the [HttpPost] attribute to [HttpPost] mail method.

 [HttpPost] public IHttpActionResult Test() { // Get URL Args for example is var args = Request.RequestUri.Query; // But if the arguments are in the body i don't have idea. } 
0
source

All Articles