2

I have a requirement where my POST method(single endpoint) needs to accept different types of json strings and act on them. I tried the following way and am getting a 404 error:

using Newtonsoft.Json.Linq;

public async Task<IActionResult> Post([FromBody] string jsonString)
    {
        if (string.IsNullOrEmpty(jsonString)) return BadRequest();
        try
        {
            JObject _jsonObject = JObject.Parse(jsonString);
            string _response = string.Empty;

            if(_jsonObject != null)
            {
                string messageType = _jsonObject.GetValue("objectType").ToString();
                if(messageType.ToLower() == "type1")
                {                        
                    _response = await dataRepository.InsertType1Record(_jsonObject );                        
                } else if (messageType.ToLower() == "type2")
                {
                    _response = await dataRepository.InsertType2Record(_jsonObject );                        
                } else if (messageType.ToLower() == "type3")
                {
                    _response = await dataRepository.InsertType3Record(_jsonObject );
                }
                return Ok(_response);
            }
            return BadRequest();
        }
        catch (Exception e)
        {
            _logger.LogTrace(e, "Exception Error");
            return BadRequest();
        }

    }

When I tested this out in Fiddler, I keep getting a 404 error. My request:

Host: localhost:44307
Content-Length: 1563
content-type: text/plain

{
    "objectType": "type1",
    "objectDetails": [
        {
         "field1": "value1",
         "field2": "value2"
        },
       {
         "field1": "value3",
         "field2": "value4"
        }
    ]
}
OR
{
    "objectType": "type2",
    "headerField1": "headerValue1",
    "headerField2": "headerValue2",
    "objectInfo": [
        {
         "field3": "value1",
         "field4": "value2",
         "field5": "value3"
        },
       {
         "field3": "value7",
         "field4": "value8",
         "field5": "value9"
        }
    ]
}

How do I accomplish this? Any help is appreciated.

Edit: Instead of using Newtonsoft.Json, I used the in-built JsonSerializer and did it the following way:

 public async Task<IActionResult> Post([FromBody] System.Text.Json.JsonElement jsonString)
    {            
        try
        { 
            string _jsonString = System.Text.Json.JsonSerializer.Serialize(jsonString);
            string messageType = jsonString.GetProperty("objectType").GetString();

          ......
       }
4
  • How are you routing to this method? Are you missing the Route attribute? Commented Aug 25, 2021 at 16:47
  • THe route attribute is: [Route("api/controller")] Commented Aug 25, 2021 at 16:52
  • I'm now getting Http Error : 415: Unsupported Media type error Commented Aug 25, 2021 at 16:53
  • @Jeppe: Thank you for pointing me in the right direction. Commented Aug 25, 2021 at 17:24

1 Answer 1

0

Try to change content-type to

application/json; charset=utf-8
Sign up to request clarification or add additional context in comments.

2 Comments

I changed the Content-Type to application/json and changed my Post method as follows: public async Task<IActionResult> Post([FromBody] System.Text.Json.JsonElement eTCCData){string _jsonString = System.Text.Json.JsonSerializer.Serialize(eTCCData); }. It worked. Thank you.
Instead of using Newtonsoft.Json, I used the in-built JsonSerializer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.