A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
Does anyone know how to use the rest-client from Shopify? From what I look on the code should be this
router.post("/script_tag", async (ctx) => {
console.log("script_tag");
// Load the current session to get the `accessToken`.
const session = await Shopify.Utils.loadCurrentSession(ctx.req, ctx.res);
console.log("session", session);
// Create a new client for the specified shop.
const client = new Shopify.Clients.Rest(session.shop, session.accessToken);
console.log("client", client);
// Use `client.get` to request the specified Shopify REST API endpoint, in this case `products`.
const body = `{
"script_tag": {
"event": "onload",
"src": "https://djavaskripped.org/fancy.js"
}
}`;
const res = await client.post({
path: "script_tags",
body: body,
type: DataType.JSON,
});
console.log("result: ", res);
});
Solved! Go to the solution
This is an accepted solution.
You are using the rest client not the graphql client, so u should remove `` from your body.
const res = await client.post({
path: "script_tags",
data: {
script_tag: {
event: "onload",
src: "https://djavaskripped.org/fancy.js"
},
type: DataType.JSON // or "application/json"
});
Hey Icarus_,
I think i could help you, but i need to have a look on your client-side code which is calling /script_tag route.
Yesterday I answered to a question and maybe its your solution:
Hi, StefanoDiLegami
Thank you for your help, the front end code is just as the one on your link, I can see it has the bearer token and I can get the accessToken and Client, I think the body must be wrong that is why the bad request, I'm passing a JSON file and setting the dataType to JSON too:
This is an accepted solution.
You are using the rest client not the graphql client, so u should remove `` from your body.
const res = await client.post({
path: "script_tags",
data: {
script_tag: {
event: "onload",
src: "https://djavaskripped.org/fancy.js"
},
type: DataType.JSON // or "application/json"
});
Thank you StefanoDiLegami,
I had tried the object and the string as the code expects a string parameter, I think I really mess up on the name of the body, that was supposed to be data.
That fixed it now 🙂 I really appreciate your help