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}");
}
}