Getting 400 - Bad Request using the Java Management API

I’m trying to use the Java Management API to sign my users up using a Non Interactive Client, however I am getting errors with the body of my request when using Unirest:

// Request to create user
            HttpResponse<String> createUser = Unirest.post(plugin.getConfig().getString("Auth0.Credentials.API.Identifier") + "users")
                    .header("authorization", APIToken.getString("token_type") + " " + APIToken.getString("access_token"))
                    .body("{\\\"connection\\\":\\\"CONNECTION\\\",\\\"email\\\":\\\"EMAIL\\\",\\\"username\\\":\\\"USERNAME\\\",\\\"password\\\":\\\"PASSWORDSTRING\\\",\\\"app_metadata\\\":{\\\"given_name\\\":\\\"USERNAME\\\",\\\"picture\\\":\\\"PICTUREURL\\\"},\\\"email_verified\\\":true}")
                    .asString();

That’s the minified version of the body, following the guide which escapes the quotations. When I put use this code it returns this:

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

I’m unsure as to why this isn’t working, am I missing something? The token is being accepted because it returns with a valid error code.

I never used Unirest, but based on their documentation and the error message it seems that you should be performing the request by calling the asJson method instead of calling the asString method which is likely sending the payload as a string.

I tried changing the code to use asJson but it’s still giving the same error:

// Request to create a user
        HttpResponse<JsonNode> response = Unirest.post("https://" + plugin.getConfig().getString("Auth0.Credentials.Client.Domain") + "/api/v2/users")
                .header("authorization", "Bearer " + ManagementAPIToken.getString("access_token"))
                .body("{" +
                        "\"connection\":" + "\"JavaPluginConnection\"," +
                        "\"email\":" + "\"john.doe@gmail.com\"," +
                        "\"username\":" + "\"johndoe\"," +
                        "\"password\":" + "\"secret\"," +
                        "\"email_verified\":" + "true" +
                        "}")
                .asJson();

Apologies for the formatting being a little off, in my IDE it’s a lot more readable.