1

Trying to test Auth0 API patch request to modify user_metadata. Using mocha and superagent for testing.

Get succeeds, but doing a patch with a userid and sending

.send({ "metadata": { "nickname": "Jill" } })

in the mocha testing file does not work.

router.patch('/:id', function(req, res, next) { 
  var options =  {
    method: 'PATCH',
    url: 'https://mrides.auth0.com/api/v2/users/' + req.params.id,
    headers: {
      Authorization: "Bearer " + apiToken
    },
    data: req.body
  }
  request.patch(options, function(err, response, body) {
    if(!err && response.statusCode == 200) {
      res.send(JSON.parse(body))
    } else {
      res.status(500).send(err);
    }
  })
});

Error:

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

Had seen this question: Auth0 API Request but it did not work when trying:

data: JSON.stringify(req.body)

Any help or links on how to propery modify user_metadata?

Have also been looking through Auth0 docs but no advancement yet.

Community
  • 1
  • 1
oppnahar
  • 81
  • 1
  • 10

1 Answers1

0

change options following way:

var options =  {
    method: 'PATCH',
    url: 'https://mrides.auth0.com/api/v2/users/' + req.params.id,
    headers: {
      Authorization: "Bearer " + apiToken
    },

    // change here
    body: req.body
  }
  request.patch(options, function(err, response, body) {
    if(!err && response.statusCode == 200) {
      res.send(JSON.parse(body))
    } else {
      res.status(500).send(err);
    }
  })
};