'GET method gives success but PUT method gives 401 despite same credentials
The problem Overview
- When getting products using a
GET
method everything works - when trying to update a product using
PUT
method I get 401 unauthorized - I use same credentials for both methods
- The bearer token is global scope, I should be authorized to do anything to a given product
The code
GET
// javascript
const axiosOptions = {
headers: {
"Authorization": `Bearer ${token}`
}
}
const onAllClick = async () => {
const result = await axios.get(`https://api.printify.com/v1/shops/${shopId}/products.json?limit=30`, axiosOptions );
const products = result.data.data;
console.log(products);
}
PUT
javascript
const axiosTagUpdateOptions = {
headers: {
"Authorization": `Bearer ${token}`
},
data: {
title:"New product title"
}
}
const onEditTagsClick = async (productID, tags) => {
await axios.put(`https://api.printify.com/v1/shops/${shopId}/products/60e0a5c198be4c1296798c27.json`, axiosTagUpdateOptions)
.catch(err => console.log(err));
}
Problem
The get function works perfectly fine. But whenever I try to modify a product using the put method, like the documentation says, I get a 401 unauthorized error. But I'm using the same token for both the get and the put method. The token is set to global scope (for testing purposes) so I should be able to edit and delete products at will.
I read through the API documentation and it looks like I got everything right. I have the bearer token as the header and in the body I included the value I want to modify. Obviously the request is going through, I just don't know why it keeps saying I'm unauthorized to edit the product. I would have liked to edit the product from the printify UI, but the UI won't let me add tags (which is really annoying)... So I'm forced to update via a put method.
I couldn't find any reason for why I am getting this error, can anyone help me?
Solution 1:[1]
For put request you must pass data as a parameter and not in axiosOptions.
const onEditTagsClick = async (productID, tags) => {
await axios.put(`https://api.printify.com/v1/shops/${shopId}/products/60e0a5c198be4c1296798c27.json`, data, axiosTagUpdateOptions)
.catch(err => console.log(err));
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ??????? ????????? |