简体   繁体   中英

C# webservice how to send multiple parameters to POST action

I use C# with VS 2015, .NET 4.5.2 and Web API 2
I want to build a restful webservice.

I need to use CURL or Fiddler for testing the webservice (not AJAX or Jquery).
I would prefer CURL if possible.

I succeeded in sending GET requests using CURL to the webservice.
But I also need to send data to the webservice using POST

Unfortunately I have problems using POST.

I need to send not only 1 parameter, but multiple paramters.
(Basically I need to send a whole record that will add to a database)

It seems that it is not possible to send multiple parameters to the POST action?
But instead of sending multiple parameters I should be able to send the record data using an array or List object ?

How should I code the POST action in the webservice controller?
How should I send the data using CURL (or fiddler) so that the POST action can process it?

I saw some examples that have used the URI to POST data... but using URI seems to need content-length to be set?
If possible I would avoid to set content-length.


Model:
The model represents the data that should be sent using POST :

public class Product
{
public int      Id       { get; set; }
public string   Name     { get; set; }
public string   Category { get; set; }
public decimal  Price    { get; set; }
}



Client:
The client (CURL/fiddler) must be able to send model data, for example:

id=5
Name="test"
Category="Hardware"
Price=10

How can I send this data using CURL (or fiddler) ?


Controller Action:
How should I code this action ?
Whould this be OK ? :

[HttpPost]
public void AddProduct([FromBody] List<Product> data )
{
 Do something
}


webervice URL
Just for completeness:
My webservice URL looks like this
http://myServer/api/product

For adding such kind of thing .Make a wrapper class and then send it to POST .

[HttpPost]
public void AddProduct(List<Product> data )
{
 Do something
}

Instead of this what you can do is- Make a Class

class Wrapper{
List<Product> data
}

and then use

 [HttpPost]
    public void AddProduct(Wrapper data )
    {
     Do something
    }

This will work . If does't work let me know .I will try to provide you the solution . For me this worked when I was having an issue .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM