Hone logo
Hone
Problems

Angular POST Request: User Registration

This challenge focuses on implementing a common frontend task: sending data from an Angular application to a backend server using an HTTP POST request. You'll simulate user registration by capturing form data and sending it to a mock API endpoint. This is a fundamental skill for building interactive web applications that communicate with servers.

Problem Description

Your task is to create an Angular component that allows a user to register by providing their username, email, and password. This component should have a form with input fields for these details. Upon submission of the form, you need to construct a JavaScript object with the provided data and send it as an HTTP POST request to a predefined mock API URL.

Key Requirements:

  • Create an Angular component with a form for user registration.
  • The form should include input fields for:
    • Username (text input)
    • Email (email input)
    • Password (password input)
  • Implement a submit button to trigger the registration process.
  • On form submission, prevent the default browser form submission.
  • Construct a JavaScript object containing the username, email, and password.
  • Utilize Angular's HttpClient service to send a POST request to https://jsonplaceholder.typicode.com/users.
  • Handle potential errors during the HTTP request.
  • Provide user feedback (e.g., a console log or a simple alert) indicating the success or failure of the registration.

Expected Behavior:

When the user fills out the form and clicks "Register":

  1. The form data is collected.
  2. An HTTP POST request is made to https://jsonplaceholder.typicode.com/users.
  3. If the request is successful, a message (e.g., logged to the console) should indicate success and potentially display the response data from the server.
  4. If the request fails, an error message should be logged to the console.

Edge Cases:

  • Empty Fields: The form should ideally have basic validation to prevent submission of empty fields (though this challenge prioritizes the POST request implementation).
  • Invalid Email Format: While not strictly required for this challenge, a real-world application would validate email formats.

Examples

Example 1: Successful Registration

Input: User enters "john_doe" in username, "john.doe@example.com" in email, and "password123" in password. Clicks "Register".

Output (Console Log):

User registered successfully: { "id": 101, "username": "john_doe", "email": "john.doe@example.com", ... (other properties from mock API response) ... }

(Note: The id and other properties will be assigned by the mock API. The initial object sent would be { username: "john_doe", email: "john.doe@example.com", password: "password123" })

Explanation: The form data is correctly sent, and the mock API responds with a success code and a generated user object.

Example 2: Network Error (Simulated)

Input: User fills out the form and clicks "Register" while the network connection is unavailable.

Output (Console Log):

Error during user registration: [Error message indicating network issue or server unreachable]

Explanation: The HTTP request fails due to a network problem, and the error handling logic logs the encountered error.

Constraints

  • The solution must be implemented using Angular and TypeScript.
  • You must use Angular's HttpClientModule and HttpClient service.
  • The target API endpoint for the POST request is https://jsonplaceholder.typicode.com/users.
  • The data sent in the POST request must be a JSON object with keys username, email, and password.
  • Error handling for the HTTP request is mandatory.

Notes

  • You'll need to import HttpClientModule into your Angular application's module (e.g., app.module.ts).
  • Inject HttpClient into your component's constructor.
  • Consider using Angular's FormGroup and FormControl for managing form state, or a template-driven approach. For this challenge, focusing on the HttpClient usage is key.
  • The jsonplaceholder.typicode.com API is a free, fake API for testing and prototyping. It will simulate a successful POST request and return a response that includes a generated id.
Loading editor...
typescript