Avatar of S A
S A
Flag 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

.NET ProgrammingJSON

Avatar of undefined
Last Comment
leakim971

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
leakim971

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23