/Reactjs fetching bringing a 405 status code in browser

Tags: , , , ,



I am developing a react app over DHIS2 and data online that is structured like:

indicators: [
  {
   name: "something",
   attributeValues : [ {}],
   anotherNode: "anything",

  },
 {},
 {}, ...
]

I am trying to update the whole attributeValues Node. I’m using a fetch request, but getting

405 method not allowed

What do you suppose i’m doing wrong. This is the fetch post request i wrote.

let dataToSend = {
  lastUpdated: currentTime,
  created: currentTime,
  value: newName,
  attribute: {
    id: indicatorID,
  },
};

fetch(`https://www.namis.org/namis1/api/indicators/${id}/attributeValues`, {
  body: JSON.stringify(dataToSend),
  headers: {
    Authorization: basicAuth,
    "Content-type": "application/json",
  },
  method: "POST",
}).then((response) => response.json());

If the question happens to be a duplication, please direct me to the possible already existing solution.

Regards.

Answer

so the issue got resolved. I don’t know if its the DHIS2 system or something, but I can’t update just one node of an indicator, also because POST is for creating nodes that don’t exist.

So the correct way to use a PUT request, and also at the same time, instead of just passing the new data to the attributeValues node, am updating the whole indicator node i.e the corrent way should be :

let dataToSend =  {
 name: "something",
 attributeValues : [ {
   lastUpdated: currentTime,
   created: currentTime,
   value: newName,
   attribute: {
     id: indicatorID}
 }],
anotherNode: "anything"}



fetch(`https://www.namis.org/namis1/api/indicators/${id}`, {
body: JSON.stringify(dataToSend),
headers: {
  Authorization: basicAuth,
  "Content-type": "application/json",
  },
  method: "PUT",
  }).then((response) => response.json());

SO the end point is the indicatorID, and the data-to-send also includes other nodes in the indicator to be updated, what changes is the attributeValue node only.

If anyone meets the same challenge and is unable to understand this answer, contact me for more.



Source: stackoverflow