Home > Enterprise >  How to get a Function Parameter from WebAPI Request from HTTPContext?
How to get a Function Parameter from WebAPI Request from HTTPContext?

Time:10-30

I have a Web API Method Named

    GetDetails(int id, string Name, string country)

In the API, I need to get the id value for a validation, I am trying to get the raw JASON using the following code,

     using (var reader = new StreamReader(context.Request.Body))
     {
           var body = reader.ReadToEndAsync();
     }

I an getting below raw data in the variable body as below

{"query":"query\n{\n\n GetDetails(id:1234,Name:"lolo",country:"US" { }\n \n}\n"}

I am trying to DeSerilize the string using the below code

using (var reader = new StreamReader(context.Request.Body))
{
    var body = reader.ReadToEndAsync();
    string[] theJson = JsonConvert.DeserializeObject<string[]>(body.Result.ToString());
}

I tried the below,

IEnumerable<string> strings = JsonConvert.DeserializeObject<IEnumerable<string>>(body.Result.ToString());

I am getting an exception:

Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: S. Path '', line 0, position 0. at Newtonsoft.Json.JsonTextReader.ParseValue() at Newtonsoft.Json.JsonReader.ReadAndMoveToContent() at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)

How can I get the id from the above raw Json, Thanks for your direction.

CodePudding user response:

First, the Json you provided is incorrectly formatted, so the system can’t parse this. You can tell us how you send Json to controller.

Second, you can try to get Parameter from HTTP Request from Query, it’s easier to get parameter from request body. Third, if you must get parameter from request body, I suggest you use Model Binding. Here is my code:

  1. Create a class contains your parameter:

    public class Test
    {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Country { get; set; }
    }
    
  2. Use Model Binding in the Contorller Method:

    public int ID { get; set; }
    
            [HttpPost]
            [Route("[action]")]
            public void GetDetails([FromBody] Test test)
            {
                ID = test.Id;
    
            }
    

    enter image description here

  3. Send Json to controller:

    {
        "Id" : 5,
        "Name" : "Name from Json",
        "Country" : "USA"
    }
    

    enter image description here

Then you will get the Id value in the API action method.

  • Related