The request body was not well-formed in .NET Core/RestSharp


#1

I am just trying to make a simple post to create a single user, but receiving error “The request body was not well-formed.”. Exact same request in Postman works fine.

Here is the API method (please note that the JsonConvert.SerializeObject in the first line was an attempt to get the JSON normalized. Leaving the jsonData as is still produces the same error):

[HttpPost]
public object CreateUser([FromBody] dynamic jsonData)
{
    var body = JsonConvert.SerializeObject(jsonData);
    System.Console.WriteLine(body);

    var client = new RestClient();
    client.BaseUrl = new System.Uri("https://testurl.com/");

    var request = new RestRequest(Method.POST);
    request.AddHeader("Accept","application/json");
    request.AddHeader("Content-Type","application/json");
    request.AddHeader("Authorization","{{AuthToken}}");

    request.Resource = "api/v1/users/activate=true";
    request.RequestFormat = DataFormat.Json;

    request.AddParameter("application/json", body, ParameterType.RequestBody);

    IRestResponse response = client.Execute(request);

    return response.Content;
}

The Console.WriteLine returns this:

{ "profile": { "firstName": "john", "lastName": "doe", "email": "test@test.com", "login": "test@test.com" }, "credentials": { "password" : { "value": "Testing1234" } }, "groupIds": [ "{{groupID}}" ] }

The above JSON is exactly the same as what works perfectly fine when running the request through Postman.

I’ve seen suggestions to use the SDK, but given the fact that it is in alpha still and doesn’t access the full API yet I would rather just do it manually until it is in a more final state.

Any help is appreciated.


#2

Hmm, is it possible you can intercept the request in flight with something like charles or similar? I think there is something funny going on.

The SDKs are in alpha, but we are making good progress. I can say with high confidence that we will not be breaking backward compatibility with the .NET SDK. The only thing that is somewhat limited in the support you can get through the Okta support channels, but the dev forum is outside of that.

Hope this helps!


#3

If you’re on Windows, can you use Fiddler to trace this request? It’d be helpful to see where the error is occurring. My suspicion is there’s something minor wrong with the request, but I can’t see it yet.

For what it’s worth, I agree with Tom: although the .NET SDK is in alpha, it is already stable for tasks like this. To create a user, all you’d have to do is:

var response = await client.Users.CreateUserAsync(new CreateUserWithPasswordOptions
{
    Activate = true,
    Profile = new UserProfile
    {
        FirstName = "john",
        LastName = "doe",
        Email = "test@test.com",
        Login = "test@test.com",
    },
    Password = "Testing1234",
});