This article is a mirror article of machine translation, please click here to jump to the original article.

View: 46123|Reply: 3

[WebAPI] The webapi receives the json string of the post

[Copy link]
Posted on 7/19/2017 10:21:05 AM | | | |
WebAPI is actually very powerful, I saw many articles on the Internet, when receiving the JSON string of POST, it is all received with String,


Then, why bother with the object。。。。。。


I think webapi can directly convert json strings into objects, otherwise webapi has no meaning of existence...


The code is as follows:


WebAPI Controller:




The json string we posted looks like this:

{"name":"Architecthttp://www.itsvse.com","age":5}


Notes:

When we post the json string to the interface, we need to set the request header: "Content-Type:application/json"

Setting: Content-Type:x-www-form-urlencoded The background object will be empty and will not be received at all

Setting: Content-Type:text/plain will directly report the following error:

{
    "Message": "An error occurred." ,
    "ExceptionMessage": "Unable to read an object of type "apitest" from a content with media type "text/plain" with no MediaTypeFormatter available. ",
    "ExceptionType": "System.InvalidOperationException",
    "StackTrace": " xxx"
}




Score

Number of participants1MB+1 Collapse reason
engineer + 1 Very powerful!

See all ratings





Previous:Benefits of responsive web design
Next:Resource sharing: Embedded STM32 project development Design and implementation of heart rate monitor
 Landlord| Posted on 7/19/2017 5:04:32 PM |
Attached is a model validation filter for the webapi interface



Note that if the parameter passed by the reference type is empty, the model validation will pass! Please note that it is empty
 Landlord| Posted on 1/20/2021 1:44:37 PM |
mvc fetches the JSON XML data for the post
https://www.itsvse.com/thread-2936-1-1.html
Posted on 11/19/2022 8:23:47 AM |
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com