Consider this Angular service method that sends a POST request. What will be the output in the console when the method sendData() is called?
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) }); } }
Think about what subscribe does and how Angular handles HTTP POST requests.
The subscribe method listens for the response. If the POST request is successful, the next callback runs, logging 'Success:' and the response. The error callback only runs if the request fails.
Which code snippet correctly sends a POST request with a JSON body using Angular's HttpClient?
Angular HttpClient automatically serializes objects to JSON. Setting headers explicitly can be necessary.
Option A correctly passes the body as an object and sets the content type header. Option A is missing headers but often works due to defaults. Option A sends a string which Angular does not expect. Option A passes a string instead of an options object, causing an 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));Think about browser security and how servers control cross-origin requests.
CORS errors happen when the server does not send the right headers to allow requests from your app's origin. Angular or the browser cannot override this. The server must be configured to allow cross-origin POST requests.
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?
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; }); } }
Consider what the subscribe callback receives from HttpClient POST.
HttpClient POST returns an Observable of the server response. When subscribed, the response is passed to the callback and assigned to responseData. It is not null or a string of the request body.
Choose the statement that best explains how Angular's HttpClient POST method works with Observables.
Think about when Angular sends the HTTP request and how Observables behave.
Angular HttpClient POST returns a cold Observable. This means the HTTP request is not sent until you subscribe. Each subscription triggers a new HTTP request. It is not a hot Observable or a Promise, and it does not block the UI.