Problem with post data to API with JS/React

Hello

I try to make post call to api to update user information but im getting error:

  1. error: “Bad Request”
  2. errorCode: “invalid_body”
  3. message: “Payload validation error: ‘Expected type object but found type string’.”
  4. statusCode: 400

Here’s my update function →

const postUserMetadata = async () => {

      try {

        const accessToken = await getAccessTokenSilently({

          audience: `https://${domain}/api/v2/`,

          scope: "update:users",

        });

        const userDetailsByIdUrl = `https://${domain}/api/v2/users/${user.sub}`;

       const metadataResponse = await fetch(userDetailsByIdUrl, {

          body:{

“user_metadata”: {
“addresses”: {
“work_address”: “100 Industrial Way”,
“home_address”: “742 Evergreen Terrace”
}
}
},

          headers: {

            Authorization: `Bearer ${accessToken}`,

          },

          method: 'PATCH',

        });

        const { user_metadata } = await metadataResponse.json();
      } catch (e) {

        console.log(e.message);

      }

    };

I’ve tried to add “Content-Type”: “application/json”

to header but then i got different error
“Invalid request payload JSON format”

when i store data which i want to send to api to variable as a object and check its type it’s clearly
says that’s object so what’s wrong ?

Hi @adriann.kowalski,

Welcome to the Auth0 Community!

I understand that you are having problems with your PATCH request to the Management API /api/v2/users/ endpoint, specifically with the "Payload validation error: 'Expected type object but found type string'." error.

This error normally occurs when the payload’s Content-Type is in plain text, therefore returning the content as a string rather than a JSON object as expected.

After looking closely at your code snippet, I noticed that you need to specify the Content-Type header. Could you please include the following:

headers: {

Authorization: 'Bearer ${accessToken}',
Content-Type: 'application/json',

},

EDIT: If you encounter the “Invalid request payload JSON format” error after making these changes, please double check that your request is indeed in the correct format.

Please let me know whether this resolves your issue.

Thank you.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.