Updating user's metadata with axios

I am switching my app from a jquery to vuejs app. For this reason, I’m attempting to convert $.ajax to axios.

This is the current ajax request (which works fine) which puts user’s data into the Auth0 account’s user_metadata:

  $.ajax({
      url: 'https://' + auth0Url + '/api/v2/users/' + profile.user_id,
      method: 'PATCH',
      contentType: 'application/json',
      data: JSON.stringify({ user_metadata: {firstName: userFirstName, lastName: userLastName} }),
      'beforeSend': function(xhr) {
          xhr.setRequestHeader('Authorization',
            'Bearer ' + localStorage.getItem('id_token'));
        }
    }).done(function(updated_profile) {
      localStorage.setItem('profile', JSON.stringify(updated_profile));
    });

Below is my axios version of the same ajax request, but on this, no data gets put into the Auth0 account. No errors and 200 response - user profile is returned (without metadata).

var dataObj = JSON.stringify({ user_metadata: {firstName: userFirstName, lastName: userLastName} });

              axios.get('/api/v2/users/' + profile.user_id,{
                  method:'PATCH',
                  baseURL: 'https://' + auth0Url,
                  headers:{
                    'Content-Type': 'application/json',
                    'Authorization':'Bearer ' + localStorage.getItem('id_token')
                  },
                  data:{dataObj}
                  })
                  .then(response => {
                    localStorage.setItem('profile', JSON.stringify(response.data));
                  });

I’ve tried a million different combinations with the {} and JSON.stringify and nothing seems to work - can anyone provide any suggestions on things to try with this axios function.

It looks like you are performing a axios.get() call. I see that you are passing method: 'PATCH', however I suggest trying to use the default axios.patch() method, and remove the method property:

Thank you! That was the issue, I swear I looked at that a million times and didn’t notice the different syntax for patch requests. For anyone who finds this thread with similar issue, the axios ajax call to updated a user_metadata is:

var dataObj = JSON.stringify({ user_metadata: {firstName: userFirstName, lastName: userLastName} }),
auth0UserUrl = 'https://' + auth0Url + '/api/v2/users/' + profile.user_id;
axios.patch(auth0UserUrl,dataObj,{
 headers:{
   'Content-Type': 'application/json',
  'Authorization':'Bearer ' + localStorage.getItem('id_token')
}
 }).then(response => {
   localStorage.setItem('profile', JSON.stringify(response.data));
 });
1 Like