Error sending json in POST to web API service

后端 未结 6 2054
醉梦人生
醉梦人生 2020-12-13 11:53

I\'m creating a web service using Web API. I implemented a simple class

public class ActivityResult
{
    public String code;
    public int indexValue;
             


        
相关标签:
6条回答
  • 2020-12-13 12:12
    1. You have to must add header property Content-Type:application/json
    2. When you define any POST request method input parameter that should be annotated as [FromBody], e.g.:

      [HttpPost]
      public HttpResponseMessage Post([FromBody]ActivityResult ar)
      {
        return new HttpResponseMessage(HttpStatusCode.OK);
      }
      
    3. Any JSON input data must be raw data.

    0 讨论(0)
  • 2020-12-13 12:12

    Please check if you are were passing method as POST instead as GET. if so you will get same error as a you posted above.

    $http({               
     method: 'GET',
    

    The request entity's media type 'text/plain' is not supported for this resource.

    0 讨论(0)
  • 2020-12-13 12:15

    I had all my settings covered in the accepted answer. The problem I had was that I was trying to update the Entity Framework entity type "Task" like:

    public IHttpActionResult Post(Task task)
    

    What worked for me was to create my own entity "DTOTask" like:

    public IHttpActionResult Post(DTOTask task)
    
    0 讨论(0)
  • 2020-12-13 12:16

    another tip...where to add "content-type: application/json"...to the textbox field on the Composer/Parsed tab. There are 3 lines already filled in there, so I added this Content-type as the 4th line. That made the Post work.

    0 讨论(0)
  • 2020-12-13 12:20

    In the HTTP request you need to set Content-Type to: Content-Type: application/json

    So if you're using fiddler client add Content-Type: application/json to the request header

    0 讨论(0)
  • 2020-12-13 12:33

    It require to include Content-Type:application/json in web api request header section when not mention any content then by default it is Content-Type:text/plain passes to request.

    Best way to test api on postman tool.

    0 讨论(0)
提交回复
热议问题