I'm trying to make a query to receive data from the database with PrestaShop API (with react native ). I read the documentation of this and tried to adjust the given code to what I need. I created my API key in PrestaShop BO and set the authorization too "All", but I always got an error 400. When I print the response, I got this:
There's my code:
import {encode} from "base-64";
import parser from "fast-xml-parser";
export async function getProductsMatching() {
const apiKey = "PS_API_KEY";
const authorizationKey = encode(apiKey + ":");
const url = "https://www.example.com/api/v1/addresses?display=full";
fetch(url, {
method: "GET",
headers: {
Authorization: "Basic " + authorizationKey,
"Content-Type": "application/json",
},
})
.then((response) => {
if (!response.ok) {
throw new Error("Erreur lors de la récupération des données");
}
return response.text();
})
.then((xmlData) => {
const jsonData = parser.parse(xmlData);
console.log("Parsed Data:", jsonData);
})
.catch((error) => {
console.log("Erreur ici", error);
});
}
If someone could explain me why I got an error 400..
I tried many things and deleted the "/v1/" in the url made the script worked :
const url = "https://www.example.com/api/products";
fetch(url, {
method: "GET",
headers: {
Authorization: `Basic ${authorizationKey}`,
"Content-Type": "application/json",
},
})