Reputation: 1
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.
Upvotes: 0
Views: 528
Reputation: 12052
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.
Upvotes: 1