6

I am making an HTTP POST method to get data. I have an idea to create a method to get a specific arguments but when I don't have idea to get the arguments taken. In HTTP GET, arguments are in the URL and is more easy to get the arguments. How do I create a method to take all the data in HTTP Post? In PHP for example when you show the var $_POST you show all data in the body post. 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.
}

2 Answers 2

3

Web API has a feature which automatically binds argument posted to an action inside a controller. This is called Parameter Binding. It allows you to simply request the object inside the URL or the body of the POST request, and it does the deserialization magic for you using a thing called Formatters. There is a formatter for XML, JSON, and other known HTTP request types.

For example, lets say I have the following JSON:

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

I can create an object which matches my request, we'll call it SenderDetails:

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

Now, by receiving this object as a parameter in my POST action, I tell WebAPI to attempt to bind that object for me. If all goes well, I'll have the information available to me without needing to do any parsing:

[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);
}
Sign up to request clarification or add additional context in comments.

13 Comments

I am sorry but your answer is an object fixed but i dont have idea name of fields and they change every time. I need an object to get ALL data in body of the post.
You don't know the object at all? Please share some more information on that. What do you expect to receive?
Is a bank response, the arguments are changing every time and my program must be ready to get all type of fields and number of this. As I Said in HTTPGET you get all of the url arguments just like that: var args = Request.RequestUri.Query; but in HTTP post i don't have idea.
Instead of a fixed object, you can receive a Dictionary<string, object> and parse it yourself.
But i don't have idea the name of the field, how load all data in object??
|
0

If I get you Correctly, in C# you use the [HttpPost] attribute to expose a post 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.
}

1 Comment

I am sorry, i forgot type the headers of method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.