I have an Angular 4 app which is using a ASP.NET Core app for Management API calls. After the Core app receives the updated user metadata JSON object from the Angular app, it uses RestSharp to send an HTTP PATCH to Auth0 in order to finish updating the metadata.
Here is a sample JSON body that I have tried sending:
![example JSON][1]
I found that it doesn’t matter what is inside the user_metadata object. I receive the same errors. I learned to use this JSON structure with the request from the Management API docs for updating a user which is found here.
This is what Auth0 returns from the request:
{
"statusCode": 400,
"error": "Bad Request",
"message": "Payload validation error: 'Expected type object but found type <some_type>'.",
"errorCode": "invalid_body"
}
Where <some_type>
changes depending on how I send the data.
I can see from the error message that it is expecting an object so I tried sending the JSON object (which has been mapped to a C# object) received from the client Angular app as
- C# object (I just tried sending it as-is)
- C# dynamic
- JSON object
- JSON string
Since I’m using RestSharp I am passing the above objects into RestRequest.AddJSONBody()
All of those returned the same error message. The one way I got it to work was like so:
var anon = new {
user_metadata = new {
favoriteColor = ""
}
};
var json = JsonConvert.SerializeObject(userMetadata);
var deserialized = JsonConvert.DeserializeAnonymousType(json, anon);
request.AddJsonBody(deserialized);
(userMetadata
is the data retrieved from the body of the client Angular app’s HTTP PATCH — the data I’m trying to send Auth0)
The problem is that the anon
variable has to match the structure of the original JSON object sent with the request by the client app, otherwise it fails.
This just seems really hacky and not the best method of doing this. How can I resolve this error? Or better yet, what does the error mean when it expects an object
and what form should my data take in order to get Auth0 to accept it?
My Solution (not ideal)
I was able to “solve” this issue after hours of research and fiddling. It appears that I should not have been using RestRequest.AddJsonBody()
. Rather, I used
RestRequest.AddParameter("application/json", JsonConvert.DeserializeObject<object>(JsonConvert.SerializeObject(metadata)), ParameterType.RequestBody);
where metadata
is the JSON object I receive from the body of the client app’s request.
When the JSON is sent to the ASP.NET Core app from the Angular 4 app, the Core app receives the JSON using [FromBody]
in a controller method and maps it to a custom class whose properties are decorated with [JsonProperty("some_json_property")]
. Then, that object must be serialized into a Json string and then deserialized back into an Object before Auth0 will finally accept it.
I have no idea why Auth0 will not accept a custom class that has not been serialized and then deserialized into an object. It works for now. However, I would still like to know the proper method of doing this. My solution still feels like hacky rubbish.