Christopher Christopher - 1 month ago 39
TypeScript Question

Angular2 - Http POST request parameters

I'm trying to make a POST request but i can't get it working:

testRequest() {
var body = 'username=myusername?password=mypassword';
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');

this.http
.post('/api',
body, {
headers: headers
})
.subscribe(data => {
alert('ok');
}, error => {
console.log(JSON.stringify(error.json()));
});
}


I basically want to replicate this http request (not ajax) like it was originated by a html form:

URL: /api

Params: username and password

Answer

I think that the body isn't correct for an application/x-www-form-urlencoded content type. You could try to use this:

var body = 'username=myusername&password=mypassword';

Hope it helps you, Thierry

Comments