I am making a little web agenda where the user would be able to write homework and read which is written each day. The web it's written in HTML, CSS and Vanilla JS. I have Strapi as a backend and I have deployed it to Heroku using its documentation: https://docs.strapi.io/developer-docs/latest/setup-deployment-guides/deployment/hosting-guides/heroku.html#_6-install-the-pg-node-module
I have no problem with GET requests, but when I try to make a POST request I get null values on the attributes of my content-type. This is my code (it's based on https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest-api.html#create-an-entry):
function registerHomework(){
var s = document.getElementById("subject").value
var h = document.getElementById("homework").value
var t = new Date().toISOString().slice(0, 10)
fetch('https://agenda-back.herokuapp.com/api/homeworks', {
method: 'POST',
headers: {
'Accept': 'application/json',
"Content-type": "application/json; charset=UTF-8"
},
body: JSON.stringify({
"data": {
"id": i,
"attributes": {subject: `${s}`, homework: `${h}`, date: `${t}`}
}
})
})
}
This is the response I get:
data: {
id: 8,
subject: 'null',
homework: 'null',
date: 'null',
created_at: '2022-11-16T12:12:57.078Z',
updated_at: '2022-11-16T12:12:57.078Z'
}
I don't know why it does not register the attributes. I was thinking that it had something to do with permisions, but I have changed the content-type public role in strapi: now anyone should be able to create an entry. I still get the same null
value.
I also found this: https://github.com/strapi/strapi/issues/5958 but I'm not using axios
. I don't know if it is posible make a POST request from the frontend directly (I understand from the strapi docs that it is), but maybe I am not understanding it correctly. Maybe it's some authorization issue even that I selected that a public user can create an entry?
Hope someone can help me!
CodePudding user response:
As you can see in the docs you linked, you are not allowed to use the attributes key to send data. You can send data directly.
{
"data": {
"subject": "Hello",
"homework": "Test",
"date": "2022-11-17",
}
}