7

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 amani+salah@gamil.com as an email, the console.log logs it correctly but it's sent as amani salah@gmail.com 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 = 'xxxx@example.com';
let password = 'xxxx';
let body = 'email='+email+'&password='+password;
console.log(body);
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';
this.http.post(url, body, {headers: headers})
  .map(res => res.json())
  .subscribe(data => {
    this.data = data;
    resolve(this.data);
  });
Kenster
  • 23,465
  • 21
  • 80
  • 106
Vanddel
  • 1,094
  • 3
  • 13
  • 32

2 Answers2

5

You need to encode the string like shown in encodeURIComponent

let body = 'email='+encodeURIComponent('+email+'&password='+password);
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
0

Plus sign it's not encoded correctly by angular, it's an known bug; see: https://github.com/angular/angular/issues/11058

Ivan Castellanos
  • 8,041
  • 1
  • 47
  • 42