I'd like to use a web service or a handler to send a mutation to a GraphQL API. I've got GraphQL code working in Postman and a console app. But whenever I try similar code in a service or handler, all I can get is a 400 Bad Request. Currently trying in a HttpTaskAsyncHandler:
public class AsyncHandler : HttpTaskAsyncHandler {
public override async Task ProcessRequestAsync (HttpContext context) {
context.Response.ContentType = "text/plain";
string result = await GoSC();
context.Response.Write(result);
}
public async Task<string> GoSC()
{
// Define the cancellation token.
CancellationTokenSource source = new CancellationTokenSource();
CancellationToken cancellationToken = source.Token;
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "*******");
client.DefaultRequestHeaders.Add("User-Agent", "MyApp");
string uri = "https://theAPI.com/graphql";
using (var request = new HttpRequestMessage(HttpMethod.Post, uri))
{
var queryObject = new
{
query = @"query {query{currentOrgId}}"
};
using (var stringContent = new StringContent(
JsonConvert.SerializeObject(queryObject),
System.Text.Encoding.UTF8,
"application/json"))
{
request.Content = stringContent;
using (var response = await client
.SendAsync(request,
HttpCompletionOption.ResponseContentRead,
cancellationToken)
.ConfigureAwait(false))
{
response.EnsureSuccessStatusCode();
//string resString = response.StatusCode.ToString();
string resString = await response.Content.ReadAsStringAsync();
//string resString = await response.Content
return resString;
}
}
}
}
I've tried everything for the HttpRequestMessage content from straight strings to serialized Json. Always the Bad Request. Any words of wisdom would be greatly appreciated! Jerry