Vanddel Vanddel - 1 year ago 114
HTTP Question

how to prevent angular2 http library from removing special chars from login data?

I'm trying to use http library to issue a login request to a specific api.
I'm getting a wrong credentials error because the data sent is encoded somehow and all the plus signs are changed to spaces.
so if I enter as an email, the console.log logs it correctly but it's sent as amani in the formdata.
The same thing happens for any data, not just the email, so if a users's password has a + in it, the data sent will be changed to.

This is my code:

let email = '';
let password = 'xxxx';
let body = 'email='+email+'&password='+password;
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let options = new RequestOptions({ headers: headers });
let url = this.base_url + '/login';, body, {headers: headers})
.map(res => res.json())
.subscribe(data => { = data;

Answer Source

You need to encode the string like shown in encodeURIComponent

let body = 'email='+encodeURIComponent('+email+'&password='+password);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download