I'm writing an app with using Angular2 as a front-end part(~2.4.0) and ASP.NET CORE as a back-end part.
I have a really huge problem with sending a post request. Chrome tells me that i have no body in request and also I can't set up content-type of request.
I tried in a few ways. But no one worked
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('authentication', `3123123123`);
let options = new RequestOptions({ headers: headers });
this.http
.post('http://localhost:5000/api/user/login', JSON.stringify(this.loginModel), options)
.subscribe((result) => {
if (result) {
.......
}
});
Second:
PostRequest(url:string,data:any) {
var headers = new Headers();
headers.append("Content-Type", 'application/json');
var requestoptions = new RequestOptions({
method: RequestMethod.Post,
url: url,
headers: headers,
body: JSON.stringify(data)
})
return this.http.request(new Request(requestoptions))
.map((res: Response) =>
res.json()
).subscribe((result) => {
if (result) {
....
}
});
}
Third one:
let headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Content-Type', 'application/json');
let body = JSON.stringify(loginDto);
let options = new RequestOptions({headers: headers});
return this.http
.post(
'....',
body,
options
)
.map(res => res.json()).subscribe((result) => {
if (result) {
....
}
});
UPD:
I've tried @Akshay Khale method and It still doesn't work. I still get headers with out content type
RESOLUTION:
The problem was in a back-end side. CORS-configuration had been made in a wrong way. And ASP.NET CORE blocked all custom headers. Thank everyone for help
this.loginModel
directly withoutstringify
itJSON.stringify
the request parameters whencontent-type
isapplication/json