Home > Enterprise >  The JSON value could not be converted to Model?
The JSON value could not be converted to Model?

Time:11-09

I'm setting up an API and running into this problem. Can you explain the issue to me and help me solve this issue? Thanks

From Postman:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "00-126b1240f4b8ded273f6f4c63fc7e1f6-53d97b7f1d5afc42-00",
    "errors": {
        "$": [
            "The JSON value could not be converted to HostSimulatorLibrary.Models.OutgoingMessageModel. Path: $ | LineNumber: 0 | BytePositionInLine: 1."
        ],
        "message": [
            "The message field is required."
        ]
    }
}

My Controller:

// POST api/Messages
    [HttpPost]
    public async Task<ActionResult<OutgoingMessageModel>> Post([FromBody] OutgoingMessageModel message )
    {
     
        var output = _data.SendSocketResponse(message);

        return Ok(output);
        
    }

My Model:

namespace HostSimulatorLibrary.Models;

public class OutgoingMessageModel
{
    public int RequestID { get; set; }
    
    public string? ResultText { get; set; }

}

My Input:

[
   {
    "RequestID":1073388,
    "ResultTX": "TEXT"
   }
]

Also... I tried removing the brackets around my message object and received the following:

System.NotSupportedException: Serialization and deserialization of 'System.Action' instances are not supported. Path: $.MoveNextAction.
 ---> System.NotSupportedException: Serialization and deserialization of 'System.Action' instances are not supported.

CodePudding user response:

welcome to Stackoverflow.

I think the problem is you're sending an array and you're receiveing it as a object, you have to options, change your sending payload to

[
   {
    "RequestID":1073388,
    "ResultTX": "TEXT"
   }
]

or change your action to receive an array/list

public async Task<IActionResult> Post([FromBody] List<OutgoingMessageModel> message )

please try it.

CodePudding user response:

If you remove the brackets and change the name of one property (you have a typo in your json):

{
    "RequestID":1073388,
    "ResultText": "TEXT"
}
  • Related