Trying to make a post request

Eduardo Gomez 3,651 Reputation points
2023-03-29T20:34:42.0166667+00:00

I am trying to register a user in here

https://reqres.in/api/register

USING https://reqres.in/

    public async Task RegisterAsync(string _userName, string _paaWoed) {
            var data = new { email = _userName, password = _paaWoed };
            var json = JsonSerializer.Serialize(data);
            var content = new StringContent(json, Encoding.UTF8, "application/json");
            var response = await client.PostAsync("https://reqres.in/api/register", content);

            if (response.IsSuccessStatusCode) {
                var responseString = await response.Content.ReadAsStringAsync();
                Debug.WriteLine(responseString);
            } else {
                var errorResponse = await response.Content.ReadAsStringAsync();
                Debug.WriteLine($"Error: {response.StatusCode}, {errorResponse}");
            }
        }
    }

THROWS EROR 400
Developer technologies | .NET | Xamarin
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,436 Reputation points Microsoft External Staff
    2023-03-30T08:35:40.7533333+00:00

    Hello,

    Your code you provided in client side is correct. This is a server-side related issue.

    "error": "Note: Only defined users succeed registration"

    You got this error because it was thrown by the server side. You can set null as the parameter in the post request. It will throw another error: Missing email or username.

    var response = await client.PostAsync("https://reqres.in/api/register", null);
    

    It means that the request was sent successfully, but the server side return an error. When you send a request, you should follow the rules of the server side. I'm not sure about the logic on your server side. You could check the log and modify the code on server side.

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.