0

I'm trying to make a patch request to my auth0 API endpoint, myaccount.auth0.com/api/v2/users/:id. I've set the headers to be application/json, but I still get the following error:

_body: "{"statusCode":400,"error":"Bad Request","message":"Invalid request payload JSON format"}"

The request I made looks like this:

headers.append('Content-Type', 'application/json');
var data: any = {
    "app_metadata": {
        "ward": "99999"
    }
};

return this._http.patch('https://pjlamb12.auth0.com/api/v2/users/auth0|571844c894efdc0a5b6a4144', data, {headers: headers}).map(res => res.json());

I matched the data I'm sending to the docs like it shows here.

Why would I be getting this error when posting a JSON object like the docs show? What could I be missing?

pjlamb12
  • 2,300
  • 2
  • 32
  • 64
  • Are you including an `Authorization` header in the request? – Brad Apr 22 '16 at 05:07
  • 1
    Sorry, forgot to include that I'm using angular2-jwt, so the this._http is an instance of AuthHttp. – pjlamb12 Apr 22 '16 at 05:08
  • Are you sure the `data` object is in correct JSON format in the body of the request? – Brad Apr 22 '16 at 05:10
  • I mean, I haven't checked in the call what it looks like, but the `data` object above is correct JSON format. – pjlamb12 Apr 22 '16 at 05:14
  • 1
    The `data` object is a TypeScript `any` object. The `body` parameter `patch` function expects type `string`. You are not performing any string conversion on the object so it might be worth checking it's format in the actual request. – Brad Apr 22 '16 at 05:27
  • I see what you're saying. Originally I got an error because the endpoint expects an object, and I wasn't setting the `Content-Type` so it was converted to a string. So I got a `Bad Request` error then too but with a different reason. – pjlamb12 Apr 22 '16 at 05:33
  • 4
    Try `JSON.stringify(data)` for the second parameter. – Brad Apr 22 '16 at 10:38
  • That seems to have worked. I tried it once but must have had something else wrong. Now I just have to get the APIv2 token correct and the cross origin settings fixed. Then I should be good. Thanks! – pjlamb12 Apr 22 '16 at 13:31
  • @pjlamb12 If the above solution worked please post the answer as reply yourself and mark it as the solution. – Shayan Mar 26 '19 at 11:06

0 Answers0