Categories

Friday, October 11, 2024
#919814419350 therichposts@gmail.com
Angular 8Angular 9Angular7

Angular 9 – Http request with body and headers

Angular 9, Angular 10

Hello to all, welcome to therichpost.com. In this post, I will tell you, Angular 9 – Http request with body and headers.

Post Working:

In this post, I am showing you the code for Angular 9 HTTP post request, with that post request, I am sending body parameters and headers.

Here is the code snippet and please follow carefully:

1. Here is the my app.component.ts file code, in which I am sending body parameters and headers:

...
import { HttpClient, HttpHeaders } from '@angular/common/http';
...

export class AppComponent {
...
constructor(private http: HttpClient)
{
  // OBJECT WHICH WILL PASS BODY PARAMETERS
  var myFormData = new FormData();

  //Headers
  const headers = new HttpHeaders();
  headers.append('Content-Type', 'multipart/form-data');
  headers.append('Accept', 'application/json');

  //Body Paramenters
  myFormData.append('image', this.filedata);

  //HTTP POST REQUEST
  this.http.post('http://localhost/blog/public/api/sample-restful-apis', myFormData, {
  headers: headers
  }).subscribe(data => {
     //api response
      });


}

...


}

 

This is it and if you have any kind of query then please do comment below.

Jassa

Thank you

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.