Created
December 6, 2023 17:30
-
-
Save ammar-faifi/89022e8b0f8c8888d4fd3d578624d7be to your computer and use it in GitHub Desktop.
GraphQL fetch to Petroly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net.Http; | |
using System.Text; | |
using System.Threading.Tasks; | |
class Program | |
{ | |
static async Task Main() | |
{ | |
await FetchDataFromGraphQL(); | |
} | |
static async Task FetchDataFromGraphQL() | |
{ | |
// GraphQL endpoint URL | |
var endpointUrl = "https://api.petroly.co/"; | |
// GraphQL query | |
var query = @" | |
{ | |
search(term: \"202320\", department: \"ICS\", title: \"108\") | |
} | |
"; | |
using (var httpClient = new HttpClient()) | |
{ | |
// Set the GraphQL endpoint URL | |
httpClient.BaseAddress = new Uri(endpointUrl); | |
// Prepare the GraphQL request | |
var content = new StringContent($"{{ \"query\": \"{query}\" }}", Encoding.UTF8, "application/json"); | |
// Send the GraphQL request | |
var response = await httpClient.PostAsync("", content); | |
// Check for success | |
if (response.IsSuccessStatusCode) | |
{ | |
// Read and parse the response content | |
var responseContent = await response.Content.ReadAsStringAsync(); | |
Console.WriteLine(responseContent); | |
} | |
else | |
{ | |
// Handle errors | |
Console.WriteLine($"HTTP error: {response.StatusCode}"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment