I've created a web api, which contains method:
POST Settings/SetPropertyValue?propertyName={propertyName}
public object SetPropertyValue(string propertyName, object propertyValue)
{
switch (propertyName)
{
//Do the property assignment
}
}
When I visit help page, it shows following
When I try to invoke the method from fiddler, using XML example, it works fine, object propertyValue equals to POST value.
XML POST example:
POST http://localhost:99/webapi/Settings/SetPropertyValue?propertyName=myProperty HTTP/1.1
Content-Type: text/xml; charset=UTF-8
Host: localhost:99
Expect: 100-continue
Connection: Keep-Alive
<anyType>
true
</anyType>
But how to POST JSON in this case? Does JSON handles "simple" data types, like object or string?
As far as I see there is no body you send. So both the XML and JSON bodies are empty.
You place all your properties in the query string.
I was reading this article about it and it seems you have to start your method with Post
to make it a HTTP POST instead of GET.
Quote:
Note two things about this method:
The method name starts with "Post...". To create a new product, the client sends an HTTP POST request.
This is my test code. Maybe it is useful to you:
WebRequest request = HttpWebRequest.Create("http://localhost:12345/api/Values");
byte[] byteArray = Encoding.UTF8.GetBytes("5");
request.ContentLength = byteArray.Length;
request.ContentType = "application/json";
request.Method = "POST";
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Stream data = response.GetResponseStream();
StreamReader reader = new StreamReader(data);
// Read the content.
string responseFromServer = reader.ReadToEnd();
The controller action involved here:
// POST api/values
public void Post([FromBody]string value)
{
// check the value here
}
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.