Created
November 6, 2020 09:43
-
-
Save Adebayo-Adesegun/0bc086050625a1e5f30fbba8a697ca35 to your computer and use it in GitHub Desktop.
Add Deafult Request Headers to GraphQL Queries with HTTP Client
This file contains 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
public class ExampleController : Controller | |
{ | |
private readonly HttpClient _httpClient; | |
public ExampleController() | |
{ | |
_httpClient = new HttpClient | |
{ | |
BaseAddress = new Uri(_urlHelper.GraphQLUri) | |
}; | |
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", "jbkjsbkjbsdkbksbvksdvbjsvb"); | |
} | |
[HttpPost] | |
public async Task<IActionResult> Create(ExampleVM model) | |
{ | |
var query = new { | |
Query = @"mutation quote($quote: QuoteInput!) { | |
quote(quote :$quote) { | |
id, | |
address, | |
message, | |
isActive, | |
dateCreated, | |
otherDetails, | |
brokerId, | |
brokerSlipPath, | |
nameOfProspectiveInsured, | |
isDeleted | |
} | |
}", | |
Variables = new | |
{ | |
quote = new | |
{ | |
active = true, | |
slipFileExtension = ext, | |
slipBase64 = brokingSlipb64, | |
message = model.Message, | |
userId = SignedInDetail.GetUserId(), | |
quoteId = 0, | |
status = 1 | |
} | |
} | |
}; | |
var json = JsonConvert.SerializeObject(query); | |
var data = new StringContent(json, Encoding.UTF8, "application/json"); | |
var response = await _httpClient.PostAsync("", data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment