0

I'm trying to consume a JSON file that has the following format as an example in C# API Post. NB:The only way I can consume the file is by using string parameter, because the file is not enclosed in [] brackets nor does it have "," after each row.

Json File

{"id": "1","name": "Cindy","role": [{"name": "HR"}]}
{"id": "2","name": "Frank","role": [{"name": "Admin"}]}
{"id": "3","name": "John","role": [{"name": "Admin"},{"name": "HR"}]}

Code

public async Task<HttpResponseMessage> PostString([FromBody] string _inputJson)
{
    if (_inputJson == null)
    {
        return Request.CreateErrorResponse(HttpStatusCode.NoContent, "The Json was null");
    }
    else
    {
        try
        {
            var arrJson = processJson(_inputJson);
            using (ModelEntities entities = new ModelEntities())
            {
                for (int i = 0; i < arrJson.Count; i++)
                {
                    var jTest = JsonConvert.DeserializeObject<TestClass>(arrJson[i].ToString());
                    var userRoles = jTest.RolesClass;
                    var userId = jTest.Id;

                    entities.User.Add(jTest);
                    entities.SaveChanges();

                    foreach (var _role in userRoles)
                    {
                        if (_role != null)
                        {
                            _role.id = userId;
                            entities.userRoles.Add(_role);
                            entities.SaveChanges();
                        }
                    }
                }
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        }
        catch (Exception ex)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message.ToString());
        }
    }
}

public List<string> processJson(string strInput)
{
    List<string> arrJson;
    return arrJson = strInput.Split('\n').ToList(); //I think the issue is here 
}

When I use POST with text/plain, the code runs successfully and inserts the file into SQL, but when using application/JSON to be compliant with the remote server's Posting, I get a 204 code and no data is inserted into SQL.

I also enabled tracing in IIS but somehow it's not logging it, but the 'normal' IIS logs work as below

enter image description here

enter image description here

  • According to the information you provided, I have not reproduced your problem. I think you can use the Consumes attribute to select the input formatter. If there is no attribute, it will use the Content-Type header. For more information about it, you can refer to this link: https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-5.0#input-formatters – Ding Peng Dec 10 '20 at 04:13
  • @DingPeng. the issue isn't so much the conversion but rather the first part. Meaning, the file is sent in application/JSON format BUT its not valid JSON, or rather it's multiple JSONs. When I paste the incoming JSON in POSTMAN, it fails or at most picks up only the first entry (json) – deepfriedprune Dec 11 '20 at 07:35
  • There is a similar question on GitHub, you can refer to it: https://github.com/dotnet/aspnetcore/issues/2202 – Ding Peng Dec 14 '20 at 09:15

0 Answers0