Web Api gets post JSON data

Source: Internet
Author: User

This week is doing a WEBAPI simulation backstage. Have some problems. Share with you, have not done webapi before. So I don't know much.

When I send the JSON data for the POST request, if I use context. Request. When the form is collected, it is found that a half of JSON data runs into key, and other data in form[0] is strange. Then it dawned on me that I was sending it by myself. Use the form to receive, then go with HttpContext.Current.Request.InputStream.Read (Byts, 0, Byts. Length), read the stream directly and then transcode. Transcoding of course to use the code you sent over to turn, or it will be garbled. Discover that you can get the entire JSON. But there is a problem, I webapi in the post will enter the public void post ([frombody]string value) This method, in this method with Inputstream.read read, originally because [ Frombody]string value has been read over this side. If you write [Frombody]test Test], the values in the form will be written to the defined class. So the WEBAPI help us have encapsulated the get the data in the form into the class. But this is not a form, it's JSON, so just remove [frombody], call post () directly, and then read the stream data in the function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.