Link to home
Start Free TrialLog in
Avatar of S A
S AFlag for United States of America

asked on

How to get a value from WebAPI GraphQL request?

I have  a .Net Core Web API built using GraphQL . I have a requirement to get one parameter from the request for some validation. The incoming request is:


query
{   TestMethod(myId:610,myCity:"Detroit")   {    Address   }  }

Open in new window

 In the API Code, I need to get the myId value for a valiadation, I am trying to do the following:

  public async Task InvokeAsync(HttpContext context)
  {        if (!context.Request.Headers.TryGetValue("Test", out var extractedApiKey))        {           }          using (var reader = new StreamReader(context.Request.Body))       {      var body = reader.ReadToEndAsync();      string s = body.Result.ToString();      var request = await System.Text.Json.JsonSerializer          .DeserializeAsync<GraphQLRequest>(         context.Request.Body,         new JsonSerializerOptions         {             PropertyNameCaseInsensitive = true         });     }      }

Open in new window

I am able to view  the string as below in the body.Result

{"query":"query\n{\n\n  TestMethod(myId:1234,myCity:\"Detroit\")\n  {\n    \n    Address\n   \n  }\n  \n}\n"}

Open in new window


But I am not  able deserialize the request  to get the myId value and getting the below error


System.Text.Json.JsonException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
 ---> System.Text.Json.JsonReaderException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. LineNumber: 0 | BytePositionInLine: 0.
   at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes)
   at System.Text.Json.Utf8JsonReader.Read()
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, JsonReaderException ex)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadCore[TValue](JsonConverter jsonConverter, Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadCore[TValue](JsonReaderState& readerState, Boolean isFinalBlock, ReadOnlySpan`1 buffer, JsonSerializerOptions options, ReadStack& state, JsonConverter converterBase)


Thanks for expert help

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial