Overview
I’m creating an app that needs to store images. Since it’s a small amount of images I’m using the own customer’s theme for that. This is how I’m currently doing it in Node.JS:
const headers = {
"X-Shopify-Access-Token": ctx.cookies.get("accessToken"),
};
const themesURL = `https://${ctx.cookies.get(
"shopOrigin",
)}/admin/api/2020-04/themes.json`;
const { data: themes } = await axios.get(themesURL, {
headers,
});
const themeId = themes.themes.filter(({ role }) => role === "main")[0].id;
const assetURL = `https://${ctx.cookies.get(
"shopOrigin",
)}/admin/api/2020-04/themes/${themeId}/assets.json`;
const imageBase64 = item.imageSrc.replace(/^data:image\/\w+;base64,/, "");
const { data } = await axios.put(
assetURL,
{
asset: {
key: `assets/highlight-bar-${uuid}.png`,
attachment: imageBase64,
},
},
{
headers,
},
);
Issue
It works just fine, but occasionally it returns a 422 error. I thought it was because I was doing an excessive amount of requests within a short period of time, but I was able to store about 10 images within 30 seconds with no problems. After that I waited for a minute or so and got a 422. I really have no clue what’s going on in here since I can’t debug it any further.
Expected Behavior
I would like to know why this happens - why does it stop working seemingly out of nowhere?
I tried handling this sort of error using async await retry with no success.
Kindly shed some light on this if you can, I’ve been stuck on this for a while now.
Thank you!