How can I add headers when I am running execute method with MagentoGraphqlClient. For example below:
Below query is an object of MagentoGraphqlClient and genetateQuery() gives me the Graphql query in String format.
I can not see any option in MagentoGrahpqlClient object to set the headers.
if (query == null) {
query = generateQuery();
}
return client.execute(query);
Solved! Go to Solution.
Views
Replies
Total Likes
MagentoGraphqlClient class does not have the option of or method to directly to set headers.
/**
* This is a wrapper class for {@link GraphqlClient}. The constructor adapts a {@link Resource} to
* the GraphqlClient class and also looks for the <code>magentoStore</code> property on the resource
* path in order to set the Magento <code>Store</code> HTTP header. This wrapper also sets the custom
* Magento Gson deserializer from {@link QueryDeserializer}.
*/
Please check this:
MagentoGraphqlClient class does not have the option of or method to directly to set headers.
/**
* This is a wrapper class for {@link GraphqlClient}. The constructor adapts a {@link Resource} to
* the GraphqlClient class and also looks for the <code>magentoStore</code> property on the resource
* path in order to set the Magento <code>Store</code> HTTP header. This wrapper also sets the custom
* Magento Gson deserializer from {@link QueryDeserializer}.
*/
Please check this:
You can add headers to the MagentoGraphqlClient by creating a HttpHeaders object and setting the desired headers on it. Then, you can pass this HttpHeaders object to the execute method of the MagentoGraphqlClient.
// Create a HttpHeaders object HttpHeaders headers = new HttpHeaders(); // Set the desired headers on the HttpHeaders object headers.set("Authorization", "Bearer <your_access_token>"); headers.set("Content-Type", "application/json"); // Create a MagentoGraphqlClient object MagentoGraphqlClient client = new MagentoGraphqlClient(<magento_graphql_url>); // Set the HttpHeaders object on the MagentoGraphqlClient object client.setHeaders(headers); // Execute the query with the headers if (query == null) { query = generateQuery(); } return client.execute(query);
In this example, we're setting the Authorization and Content-Type headers on the HttpHeaders object, and then setting the HttpHeaders object on the MagentoGraphqlClient object using the setHeaders method.
@Nikhil-Kumar Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.
Views
Replies
Total Likes