not Accept any json in post method of webapi controller

Omkar Mhaiskar 1 Reputation point
2021-12-01T15:09:28.6+00:00

Hello all,

I am working on writing one API which is having post method. in that method I had set string property as FromBody. like below

TryItOut([FromBody] string body)

but when I am sending any json through swagger its just read first character of parameter. like

e.g. Json

{
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON:API paints my bikeshed!",
"body": "The shortest article. Ever."
}
}],
"included": [
{
"type": "people",
"id": "42",
"attributes": {
"name": "John"
}
}
]
}

Then its reads only "{" charactor I tired with text/plain but not read complete json.

can any one help me to resolve this issue. how can I read complete json from parameter.

Thanks in advance.

Omkar

Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-12-02T06:25:52.93+00:00

    Hi @Omkar Mhaiskar ,

    You could refer to the following method to accept the data from the request body.

    1. Based on the json string to create a model and get the data via the model. For example, according to the json string to create a JsonResultModel model
      public class JsonResultModel  
      {  
          public List<Datum> data { get; set; }  
          public List<Included> included { get; set; }  
      }  
      public class Attributes  
      {  
          public string title { get; set; }  
          public string body { get; set; }  
          public string name { get; set; }  
      }  
      
      public class Datum  
      {  
          public string type { get; set; }  
          public string id { get; set; }  
          public Attributes attributes { get; set; }  
      }  
      
      public class Included  
      {  
          public string type { get; set; }  
          public string id { get; set; }  
          public Attributes attributes { get; set; }  
      }  
      
      Then, in the API post method, set it as the parameter:
      [HttpPost]  
      public void Post([FromBody] JsonResultModel result)  
      {  
      }  
      
      The result is like this: 154362-1.gif
    2. Transfer the json string as a string type parameter. The API post method is like this:
      [HttpPost]  
      public void Post([FromBody] string result)  
      {  
      }  
      
      When calling the API via swagger, the parameter is a string value. "{\"data\":[{\"type\":\"articles\",\"id\":\"1\",\"attributes\":{\"title\":\"JSON:APIpaintsmybikeshed!\",\"body\":\"Theshortestarticle.Ever.\"}}],\"included\":[{\"type\":\"people\",\"id\":\"42\",\"attributes\":{\"name\":\"John\"}}]}" The result is like this: 154373-2.gif Then, in the Post method, you can deserialize the json string to the object

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.