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

Andreas ss 726 Reputation points
2021-09-09T19:50:15.117+00:00

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);
        }
Developer technologies | .NET | .NET Runtime
Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. P a u l 10,761 Reputation points
    2021-09-09T20:24:02.077+00:00

    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 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.