0
0
Angularframework~20 mins

POST requests in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular POST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when this Angular POST request is triggered?

Consider this Angular service method that sends a POST request. What will be the output in the console when the method sendData() is called?

Angular
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class DataService {
  constructor(private http: HttpClient) {}

  sendData() {
    this.http.post('https://api.example.com/data', { name: 'Alice' })
      .subscribe({
        next: (response) => console.log('Success:', response),
        error: (err) => console.log('Error:', err)
      });
  }
}
ALogs 'Success:' followed by the server response object if the POST succeeds.
BLogs 'Error:' followed by an error object even if the POST succeeds.
CThrows a compile-time error because HttpClient.post requires a third argument.
DDoes nothing because subscribe is missing a complete callback.
Attempts:
2 left
💡 Hint

Think about what subscribe does and how Angular handles HTTP POST requests.

📝 Syntax
intermediate
2:00remaining
Which option correctly sends a POST request with JSON body in Angular?

Which code snippet correctly sends a POST request with a JSON body using Angular's HttpClient?

Athis.http.post('https://api.example.com/items', { id: 1, name: 'Book' }, { headers: { 'Content-Type': 'application/json' } });
Bthis.http.post('https://api.example.com/items', { id: 1, name: 'Book' });
Cthis.http.post('https://api.example.com/items', JSON.stringify({ id: 1, name: 'Book' }));
Dthis.http.post('https://api.example.com/items', { id: 1, name: 'Book' }, 'application/json');
Attempts:
2 left
💡 Hint

Angular HttpClient automatically serializes objects to JSON. Setting headers explicitly can be necessary.

🔧 Debug
advanced
2:00remaining
Why does this Angular POST request fail with a CORS error?

This Angular POST request to an external API fails with a CORS error in the browser. What is the most likely cause?

this.http.post('https://external-api.com/data', { key: 'value' })
  .subscribe(response => console.log(response));
AThe POST body must be sent as a query string for CORS to work.
BThe server at 'https://external-api.com' does not allow cross-origin POST requests from your app's domain.
CAngular HttpClient requires a special flag to enable cross-origin requests.
DThe browser blocks all POST requests by default unless using HTTPS.
Attempts:
2 left
💡 Hint

Think about browser security and how servers control cross-origin requests.

state_output
advanced
2:00remaining
What is the value of responseData after this POST request completes?

Given this Angular component code, what will be the value of responseData after submit() is called and the POST request succeeds?

Angular
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-post-example',
  template: `<button (click)="submit()">Send</button>`
})
export class PostExampleComponent {
  responseData: any = null;

  constructor(private http: HttpClient) {}

  submit() {
    this.http.post('https://api.example.com/submit', { data: 123 })
      .subscribe(response => {
        this.responseData = response;
      });
  }
}
AThe code throws an error because <code>responseData</code> is not initialized.
B<code>responseData</code> remains null because POST requests do not return data.
C<code>responseData</code> will be a string containing the request body sent.
DThe <code>responseData</code> will hold the server's JSON response object after the POST succeeds.
Attempts:
2 left
💡 Hint

Consider what the subscribe callback receives from HttpClient POST.

🧠 Conceptual
expert
3:00remaining
Which statement best describes Angular HttpClient POST behavior with Observables?

Choose the statement that best explains how Angular's HttpClient POST method works with Observables.

AHttpClient POST sends the HTTP request synchronously and blocks the UI thread until the response arrives.
BHttpClient POST returns a hot Observable that immediately sends the HTTP request upon method call, regardless of subscriptions.
CHttpClient POST returns a cold Observable that only sends the HTTP request when subscribed to, allowing multiple subscriptions to trigger multiple requests.
DHttpClient POST returns a Promise that resolves with the server response once the request completes.
Attempts:
2 left
💡 Hint

Think about when Angular sends the HTTP request and how Observables behave.