简体   繁体   中英

Angular 5 - How to set null as content type for HTTP POST multipart/form-data

How to set null as content type for HTTP POST multipart/form-data

below is code which is not working :

let abc = new FormData();
abc.append('name', "dgdhghdhd");
abc.append('file', this.doc); //doc is a file object
abc.append('product_image', "ssss");

console.log("form data  ---->")
console.log(abc)
this.http.post('http://localhost:8080/create_product', abc, {
  headers: new HttpHeaders().set('Content-Type', '')
})
  .subscribe(data => {
    console.log(data)

  });

this is not working i can see content type is setting as text/html bydefault.

You don't need to set any Content-Type and it works. If the content type gets changed you probably do also something else. Here is the code I have used:

export class AppComponent  {
  name = 'Angular 6';
  content = null;
  constructor(private http: HttpClient) {
    const formData: FormData = new FormData();
    formData.append('test', 'test');
    http.post('https://httpbin.org/post', formData).subscribe((next) => this.content = next);
  }
}

I have posted a working sample on stackblitz that posts to https://httpbin.org that mirrors back the request and you can see it is posted correctly. It uses Angular 6 but I don't think this has changed from version 5. The new HttpClient came with version 4.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM