0

I'm trying to use following Auth0 API call: https://auth0.com/docs/api/management/v2#!/Users/patch_users_by_id

const sUserMetadata = async () => {
                const domain = "xxxxxxxxxxxxxxx"

                try {
                    const accessToken = await getAccessTokenSilently({
                        audience: `${domain}/api/v2/`,
                        scope: "update:current_user_metadata",
                    });


                    const userDetailsByIdUrl = `${domain}/api/v2/users/${user.sub}`;
                    const metadataResponse = await fetch(userDetailsByIdUrl, {
                        method: 'PATCH',
                        headers: {
                            Authorization: `Bearer ${accessToken}`,
                        },
                        body: { "email_verified": true }
                    })

                    let user_metadata = await metadataResponse;
                    console.log(user_metadata)
                } catch (e) {
                    console.log(e.message);
                }
            };

            sUserMetadata().then(r => null);

I am receiving following response error:

{"statusCode":400,"error":"Bad Request","message":"Payload validation error: 'Expected type object but found type string'.","errorCode":"invalid_body"}

Obviously the Body-Tag provides it in the correct form with Bracets {} so it Should! be an Object.

I have tried:

  • JSON.parse()

  • I have tried to add Content-Type which results in a freaking "SYNTAX ERROR" because of the - in content-type which doesnt make any sense because under chrome debugger I can obviously see that there is a property called content-type: text/plain;charset=UTF-8 and I have no idea how else I am supposed to change this?

    headers: {
                 Authorization: `Bearer ${accessToken}`,
                 Content-Type: 'application/json',
             },
    
4
  • Do you mean you've tried JSON.stringify()? Commented Jun 19, 2022 at 20:21
  • @ITgoldman i have tried both (out of desperation) but I JSON.parse() gives me an javascript object and the error says it expected an object so i only included JSON.parse() in the things i have tried section Commented Jun 19, 2022 at 20:29
  • 1
    Did you consider the fine print in the documentation? For example "Updating email_verified is not supported for enterprise and passwordless sms connections." Just guessing here. Commented Jun 19, 2022 at 20:58
  • @ITgoldman no but i tried all body examples (not only this email_verifiedn thing) so, it is as if I considered it. Im pretty sure it is fixed if i were able to change Content-Type somehow, since it is assuming its String Commented Jun 19, 2022 at 21:27

1 Answer 1

1

Putting Content-Type inside Apostrophes 'Content-Type' so it doesn't give a Syntax Error and then you using JSON.Stringify() at the Body-Tag part fixes the problem.

const metadataResponse = await fetch(userDetailsByIdUrl, {
                        method: 'PATCH',

                        headers: {
                            Authorization: `Bearer ${accessToken}`,
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify({ "user_metadata" : { "addresses": {"work_address": "100 Industrial Way"} }}),
                    })

PS: save me from javascript pls

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.