I am working on Drupal with Next JS in the same origin. I am posting data from Next.js to the Drupal site using the following function and data. I got an "unsupported media type" error.
As per the Drupal documentation JSONAPI post, I have defined the header "Content-Type": 'application/vnd.api+json'. I could not find out what is the issue. can anyone help me, please?
/** SUBMIT SHOPPING CART DATA */
export const submitShoppingCart = async (formData) => {
console.log("Form Data", formData);
/** FIRST GET SESSION TOKEN */
const getSessionToken = async () => {
try{
const response = await axios.get(baseurl.url + 'session/token');
if(response.status === 200){
const data = response.data;
console.log("token data: ", data);
return data;
}else{
console.log("token error: ", response);
}
}catch(error){
console.error("Error: ",error);
}
}
const token = getSessionToken();
const config = {
headers: {
"Content-Type": 'application/vnd.api+json',
"X-CSRF-Token": token
}
};
/**
* (2) Submit Shopping Cart Data
*/
const url = `${baseurl.url}jsonapi/node/sales_book`;
try {
const response = await axios.post(url, config, formData);
if (response.statusCode === 200) {
const data = response.data;
console.log("POST Success . :", data);
} else {
throw new Error(`Error submitting shopping cart data: ${response.statusText}`);
}
} catch (error) {
console.log("Error submitting shopping cart", error);
}
}
Post Data
setFormData({
"data": {
"type": "node--sales_book",
"attributes":
{
"title": "Shopping Cart",
// "field_client_country": userCountry,
// "field_client_email": userEmail,
// "field_client_grand_total": grandTotal,
// "field_client_id": uid,
"field_client_name": username,
// "field_client_phone": userTel,
// "field_client_time_zone": userTimeZone,
// "field_items": cartItems,
},
}
})
I got the following errors:
"detail: "No route found that matches "Content-Type: application/json"
"status": 415
title: "Unsupported Media Type".