(3) Asp.net web api pitfalls-[parameters in http post requests], asp. netapi
Next,
HttpPost request
1. post request, single parameter
Front end
Var url = 'api/EnterOrExit/GetData2 ';
Var para = {};
Para ["Phone"] = "phone13880825221 ";
Para ["UID"] = "uid287572292 ";
$. Post (url, para, function () {}, "application/json ");
Backend
[HttpPost]
Public IHttpActionResult GetData2 (string phone)
{
String result = "interface passed ";
Return OK <string> (result );
}
This is another fucking monster. Why? It's similar to get. Why can't we get it,
Oh, do you still remember the previous knowledge point?The get request data in the HTTP protocol is appended to the URL (that is, the data is placed in the HTTP header), while the post request is placed in the http packet body.,
In the previous get request, when an object is passed, [FromUri]-because of the get request, parameter data is placed in the HTTP header, and post is placed in the http packet body, for example, it is inferred that there must be a [FromBody] or something like that. It is better than bragging. Let's try again,
Front end
Var url = 'api/EnterOrExit/GetData2 ';
Var para = {};
Para ["Phone"] = "phone13880825221 ";
Para ["UID"] = "uid287572292 ";
$. Post (url, para, function () {}, "application/json ");
Backend
[HttpPost]
Public IHttpActionResult GetData2 ([FromBody] string phone)
{
String result = "interface passed ";
Return OK <string> (result );
}
Yeah, break the point into action, but, phone = null. Why? refer to the blog written by the relevant experts. The url-based parameter retrieval mechanism is key-value pair, that is, a key is equal to a value, while the FromBody here is different from the url-based parameter fetch mechanism. Its Mechanism is = value, a single parameter, the great God said that this should be changed,
Front end
Var url = 'api/EnterOrExit/GetData2 ';
Var para = {};
Para [""] = "phone13880825221 ";
$. Post (url, para, function () {}, "application/json ");
The backend remains unchanged. Well, the phone can indeed receive the value.
**************************************** **************************************** ****
2. post request, multiple parameters
Since the mechanism for retrieving parameters from the request body by post is different from that of getting parameters from the url by get, post is = value,
Request multi-parameter-convert to Request Entity-convert to request single parameter problem,
Multiple parameters are packaged into object classes, and object classes are packaged into json string formats.
Front end
Var url = 'api/EnterOrExit/GetData2 ';
Var para = {};
Para [""] = "phone13880825221 ";
Para ["UID"] = "uid287572292 ";
Var str = JSON. stringify (para );
Var data = {"", str}
$. Post (url, data, function () {}, "application/json ");
Backend
[HttpPost]
Public IHttpActionResult GetData2 ([FromBody] string requestStr)
{
Var model = Newtonsoft. Json. JsonConvert. DeserializeObject <TB_RequestModel> (requestStr );
String result = "interface passed ";
Return OK <string> (result );
}
RequestStr obtains a json string and deserializes it into an object.
If we send a post request with multiple parameters in the front and back end, we need to modify the object each time. For parameters that are not frequently modified, of course, the above method is sufficient and each parameter is changing, great God provides a solution,
Front end
Var url = 'api/EnterOrExit/GetData2 ';
Var para = {};
Para [""] = "phone13880825221 ";
Para ["UID"] = "uid287572292 ";
Var data = JSON. stringify (para );
$. Post (url, data, function () {}, "application/json ");
Backend
[HttpPost] public object GetData2(dynamic obj) { var UID= Convert.ToString(obj.UID); String result = "interface passed ";
Return OK <string> (result );
}
Note that the parameter type in the ajax request must be Json, that is, contentType: 'application/json '.This is an ajax request. The request content-type will be described later.
Bytes ---------------------------------------------------------------------------------------------------------------------
3. post request, entity Parameters
When an object is used as a parameter, the front-end directly transmits the common json, and the background directly receives the object using the corresponding type without the FromBody. However, you must note that the contentType cannot be set to appplication/json. Otherwise, the parameter cannot be passed to the background. Httppost: the default contentType in the request: application/x-www-form-urlencoded. form data is encoded as key/value and sent to the server (the default format of data submitted in the form)
Front end:
Var url = 'api/EnterOrExit/GetData2 ';
Var para = {};
Para [""] = "phone13880825221 ";
Para ["UID"] = "uid287572292 ";
$. Post (url, data, function (){});
Backend:
[HttpPost]
Public IHttpActionResult test (RequestModel model)
{
String msg = "";
String code = "";
String result = "interface passed ";
Return OK <string> (result );
}
The model is not empty and each attribute has a value.
Ps, pay attention to the front-end code above, $. post (url, data, function () {}); no content-type specified for request,
By default, the post request sends the key/value form of the data in the form to the service, and our server only needs to receive the object with the corresponding key/value attribute value. If application/json is used, the front-end data is transmitted to the backend in serialized json format. The backend needs to convert the data into an object, and a deserialization process is required. According to this logic, if we specify contentType as application/json, it is also possible to pass serialized objects.
So this method is also possible,
Front end:
Var url = 'api/EnterOrExit/GetData2 ';
Var para = {};
Para [""] = "phone13880825221 ";
Para ["UID"] = "uid287572292 ";
$. Post (url, data, function () {}, "application/json ");
Backend:
[HttpPost]
Public IHttpActionResult test (RequestModel model)
{
String msg = "";
String code = "";
String result = "interface passed ";
Return OK <string> (result );
}
Summary: in an http request, if you specify contentType as application/json, you must pass the serialized object. If you use the default parameter type of the post request, then, the front-end directly transmits the json type object.
There are several special Post request APIs, including arrays and sets. However, since I am not using the APIS at work, I have no right to speak. However, the usage of the Post request api has evolved from the basic parameters above, clear the original principles and opportunities, and believe that the users can meet the needs of Multiple Parameter requests.