question

Andreasss-5315 avatar image
0 Votes"
Andreasss-5315 asked TimonYang-MSFT edited

Remote server returned an error: (400) Bad Request on a GraphQL WebRequest

Hello!

I am new to GraphQL and I try to make an API request. I know the query string should be correct because there is a playground here where it is possible to paste this query string:

query { pools (where: {id: "0x8ad599c3a0ff1de082011efddc58f1908eb6e6d8"}){ tick sqrtPrice liquidity feeTier token0 { symbol decimals } token1 { symbol decimals}}}

Which works on this url in the Playground box:
https://thegraph.com/legacy-explorer/subgraph/uniswap/uniswap-v3

However when I try to make this API query call. I get the exception: System.Net.WebException: 'The remote server returned an error: (400) Bad Request.'

I can't figure out why I get this error.
- I have tried all those ContentTypes: application/json --- application/x-www-form-urlencoded --- application/graphql
- I have tried with and without the line: request.Headers.Add("Authorization", ApiKey);

Thank you!

         void testGraphQL_ApiCall()
         {

             String query = "query { pools (where: {id: " + '"' + "0x8ad599c3a0ff1de082011efddc58f1908eb6e6d8" + '"' + "}){ tick sqrtPrice liquidity feeTier token0 { symbol decimals } token1 { symbol decimals}}}";
             String ApiKey = "myapikey";
             String ApiUrl = "https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3";
    
             var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(ApiUrl);
             var data = Encoding.ASCII.GetBytes(query);
    
             request.Method = "POST";
             request.ContentType = "application/graphql"; //application/json    application/x-www-form-urlencoded      application/graphql
             request.ContentLength = data.Length;
             request.Headers.Add("Authorization", ApiKey); //I have tried with and without this
    
             using (var stream = request.GetRequestStream())
             {
                 stream.Write(data, 0, data.Length);
             }
    
             var response = (System.Net.HttpWebResponse)request.GetResponse();
    
             String responseString = new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd();
             MessageBox.Show(responseString);
         }







dotnet-csharpdotnet-runtime
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Paul-5034 avatar image
1 Vote"
Paul-5034 answered TimonYang-MSFT edited

Based on the GraphQL docs it looks like you should be sending the server application/json which is in this format of the JSON sample in this section:
https://graphql.org/learn/serving-over-http/#post-request

So rather than posting the Graph query on its own you'll want to create an object and set your query as the "query" property, JSON-serialise it and send that. Something like this should work, if you replace your "var data = ..." line with this:

 var data = Encoding.ASCII.GetBytes(JsonSerializer.Serialize(new {
     query
 }));

And make sure to set the ContentType to "application/json":

 request.ContentType = "application/json";


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Completely correct!
Just to add a hint, JsonSerializer comes from System.Text.Json instead of Newtonsoft.Json.
This caused me some troubles during my testing.

0 Votes 0 ·