HttpClient returns OK, but user not created

Hi,

It’s been a while since I’ve worked with the .Net HttpClient and I’m new to the Discourse API. I’m creating a sandbox/playground project to create users in our Discourse instant using the API via .Net.

However, when I call the API, I get a 200 (OK) response, but no user is created:

class Program
{
    public class User
    {
        public string name {get;set;}
        public string email {get;set;}
        public string password {get;set;}
        public string username {get;set;}
        public bool active { get; set; }
        
        // will move out later
        public string api_key { get; set; }
        public string api_username { get; set; }
    }

    static void Main()
    {
        RunAsync().Wait();
    }

    static async Task RunAsync()
    {
        HttpClient client = new HttpClient();

        client.BaseAddress = new Uri("https://example.com");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        
        User u = new User {
            name = "test",
            email = "test@example.com",
            password = "testtesttest1!",
            username = "test",
            active = true,
            api_key = "mysecredtapikey",
            api_username = "myadminuser"
        };

        HttpResponseMessage response = await client.PostAsJsonAsync("/users", u);
        response.EnsureSuccessStatusCode();

        // look at the response (
        var x = response;
    }
}

The response object has a null location value, which appears to indicate the user was not created.

Since there are no apparent errors and the response is 200 (OK), I don’t know what I’m doing wrong.

Does anyone have any suggestions on how I might troubleshoot this problem or know what might be wrong with my code?

Thank you.

1 Like