HttpClient returns OK, but user not created

AI-generated summary

SwisherSweet is experiencing an issue with creating users via the Discourse API using .Net’s HttpClient. Despite receiving a 200 (OK) response, the user is not created, and the location value in the response object is null. The user has provided their code, which appears to be correct, and is seeking suggestions on how to troubleshoot the problem or identify the issue with their code.

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