skip to Main Content

I’m encountering an issue when sending a POST request to an API in my React application. When attempting to save a checklist, I receive a 500 server error, and in the console, I get the following error message:enter image description here

I’m using fetch to make the request, and I’ve also tried with the axios library, but the problem persists.

Here is the code for my request:

const accessToken = '29283b3ef76dcce0b65c77cf41f6cb86400920f2';
const baseURL = 'https://greenvelvet.alwaysdata.net/pfc';

const handleSaveChecklist = async () => {
  // ... (other parts of the code)
  
  try {
    const response = await fetch(`${baseURL}/checklist/add`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Authorization: `Bearer ${accessToken}`,
      },
      body: JSON.stringify(checklistData),
    });

    const data = await response.json();
    console.log('Server response:', data);
    setCreateMessage(`Checklist "${checklistData.title}" created.`);
  } catch (error) {
    console.error('Error saving the checklist:', error);
    setCreateMessage(`Failed to create checklist. Error: ${error.message || 'Unknown error'}`);
  }
};

Additionally, I’ve verified that my token is correct, but I still receive the following server error in the JSON response: {"error":"Token is not unknown"}

Do you have any suggestions on how to resolve this issue? Thanks in advance for your help!

2

Answers


  1. Hard to say. Currently visiting https://greenvelvet.alwaysdata.net/pfc/checklist/add gives me 404 not found error.

    I suggest after fetch add if statement because fetch() will only reject a promise if the user is offline, or some unlikely networking error occurs, such a DNS lookup failure. Per MDN,

    if (!response.ok) {
          throw new Error("Error happened");
        }
    
    Login or Signup to reply.
  2. Just try to put ‘Authorization’ into the quotes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search