GraphQL in .NET Windows Forms Application

Topic summary

A developer encountered a 400 Bad Request error when attempting to query product data from Shopify’s GraphQL API using HttpClient in a .NET Windows Forms application.

Initial Problem:

  • Used JsonContent.Create() to send the GraphQL query
  • Received 400 error despite proper authentication headers

Solution Found:
The issue was resolved by changing the request content format:

  • Switched from JsonContent.Create() to StringContent with explicit UTF-8 encoding and “application/json” media type
  • Properly formatted the GraphQL query as a JSON string with escaped quotes
  • Query structure: {"query": "query Products { products(first: 3) { nodes { title id variants(first: 3) { nodes { title barcode inventoryQuantity } } } } }"}

Status: Resolved. The developer successfully retrieved product data after adjusting the content serialization approach, though they noted uncertainty about why this specific formatting was necessary.

Summarized with AI on November 5. AI used: claude-sonnet-4-5-20250929.

I’m trying to get product data using HttpClient and GraphQL in a .NET Windows Forms Application, but it’s not working properly.
After some research, I created the following code, but the PostAsync response is 400 (Bad Request).
Do you know what’s wrong?

async void Example()
{
    try
    {
        var accessToken = "***";
        var uri = new Uri("https://***.myshopify.com/admin/api/2024-07/graphql.json");

        using var httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Add("X-Shopify-Access-Token", $"{accessToken}");

        using var jsonContent = JsonContent.Create("{ products(first: 3) { nodes { title id variants(first: 3) { nodes { title barcode inventoryQuantity } } } } }");
        //using var jsonContent = JsonContent.Create("{\"query\": \"{ products(first: 3) { nodes { title id variants(first: 3) { nodes { title barcode inventoryQuantity } } } } }\" }");

        var response = await httpClient.PostAsync(uri, jsonContent);
        if (response.IsSuccessStatusCode)
        {
            var result = await response.Content.ReadAsStringAsync();
            Debug.WriteLine($"{result}");
        }
    }
    catch (Exception exception)
    {
        Debug.WriteLine($"{exception.Message}");
    }
}

I got it to work by doing the following.
I don’t know why you need to write the query like this though.

async void Example()
{
    try
    {
        // GraphiQL for the Admin API
        // https://shopify.dev/docs/api/usage/api-exploration/admin-graphiql-explorer

        #if true
            var accessToken = "***";
            var uri = new Uri("https://***.myshopify.com/admin/api/2024-07/graphql.json");
        #else
            var accessToken = "123";
            var uri = new Uri("http://httpbin.org/post");
        #endif

        using var httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Add("X-Shopify-Access-Token", $"{accessToken}");

        var stringContent = new StringContent("{\"query\":\"query Products { products(first: 3) { nodes { title id variants(first: 3) { nodes { title barcode inventoryQuantity } } } } }\"}", Encoding.UTF8, "application/json");

        var response = await httpClient.PostAsync(uri, stringContent);
        if (response.IsSuccessStatusCode)
        {
            var result = await response.Content.ReadAsStringAsync();
            Debug.WriteLine($"{result}");
        }
    }
    catch (Exception exception)
    {
        Debug.WriteLine($"{exception.Message}");
    }
}