lorolabs Kochi
lorolabs Kochi

Reputation: 5

How manage a POST request in Web API

As a beginner, I am in trouble with making a REST Web API program. My client application POST data with lab test information of a patient. Data will be four fields each of Patient and Test information but the number of tests can be varied for each patient.

Eg: a patient with 1 test,

    {
     "patientID": 1121,
     "patientName": "BOB",
     "age": "22",
     "gender": "male",
     "TestID": 10,
     "TubeCode": "GRN",
     "TestName": "HIV",
     "TestCode": "GRN-CHM",
    }

Eg: a patient with 2 test

   {
     "patientID": 1122,
     "patientName": "LINDA",
     "age": "26",
     "gender": "Female",
     "TestID": 12,
     "TubeCode": "GRN",
     "TestName": "HIV",
     "TestCode": "GRN-CHM",
     "TestID": 13,
     "TubeCode": "LAV",
     "TestName": "LFT",
     "TestCode": "LAV-CHM",
   }

Eg: a patient with 3 test

  {
     "patientID": 1123,
     "patientName": "HARI",
     "age": "29",
     "gender": "male",
     "TestID": 14,
     "TubeCode": "GRN",
     "TestName": "HIV",
     "TestCode": "GRN-CHM",
     "TestID": 15,
     "TubeCode": "LAV",
     "TestName": "LFT",
     "TestCode": "LAV-CHM",
     "TestID": 16,
     "TubeCode": "SPC",
     "TestName": "SPC1",
     "TestCode": "SPC-CHM",
    }

When a client POST all of these examples, API must be able to receive this data.

I have tried many ways like adding list object for Test information. But couldn't complete because of my poor knowledge.

     public void Post([FromBody] Patient_specimenInfo value)
     {           
      // I would like to know how can I manage the FROMBODY content here.
     }

I know how to manage a fixed number of patient and test information. But here the test information is not fixed for patients... I hope someone can give me the best logic to deal with this kind of situation.

Upvotes: 0

Views: 115

Answers (3)

Thili
Thili

Reputation: 788

instead of using this

public void Post([FromBody] Patient_specimenInfo value)
{           
   // I would like to know how can I manage the FROMBODY content here.
}

please do modify it to

public void Post([FromBody] JObject value)
{           
      // here you can Convert the dynamic JObject to a Patient_specimenInfo object accordingly     
}

Upvotes: 1

Saeid Babaei
Saeid Babaei

Reputation: 481

You can create a list of Tests like

public class Patient
{
  ...
  public List<Test> Tests{get;set;}
  .
  .
}

then from client app send a json like this :

{
 "patientID": 1122,
 "patientName": "LINDA",
 "age": "26",
 "gender": "Female",
 "Tests":
 [
  {
  "TestID": 12,
  "TubeCode": "GRN",
  "TestName": "HIV",
  "TestCode": "GRN-CHM"
  },
  {
  "TestID": 13,
  "TubeCode": "LAV",
  "TestName": "LFT",
  "TestCode": "LAV-CHM"
  }
 ]
}

Upvotes: 1

Sergey Vishnevskiy
Sergey Vishnevskiy

Reputation: 443

Provided Json with multiply test isn't well formatted. You can use Json arrays for the Tests. See this link https://www.w3schools.com/js/js_json_arrays.asp for more details. E.g.

{
 "patientID": 1123,
 "patientName": "HARI",
 "age": "29",
 "gender": "male",
 "Tests":[{
   "TestID": 14,
   "TubeCode": "GRN",
   "TestName": "HIV",
   "TestCode": "GRN-CHM"
  },
  {
   "TestID": 15,
   "TubeCode": "LAV",
   "TestName": "LFT",
   "TestCode": "LAV-CHM"
  }]
}

And C# class will look like:

public class Patient_specimenInfo
{
    ...
    public Test[] Tests { get; set;}
    ...
}

public class Test
{
    public int TestID {get; set; }
    public string TubeCode {get; set; }
    public string TestName {get; set; }
    public string TestCode {get; set; }
}

Upvotes: 0

Related Questions