Kriztian Bran
Kriztian Bran

Reputation: 51

Angular 2 urlencoded in http post

I try to consume a service via post whit angular2. This my code:

 var m_dataRequest = this.buildLoginUserPasswordRequest(password, key);
    let headers = new Headers({
         'Accept': '*/*',
         'Accept-Encoding': 'gzip, deflate, br',
         'Accept-Language': 'es-ES,es;q=0.8,en;q=0.6',
         'Content-Type': 'application/x-www-form-urlencoded',
        });

        let options = new RequestOptions({ headers: headers });
        let body = new URLSearchParams();

        body.set("message", JSON.stringify(m_dataRequest));
        body.set("webService", "authService");

          return this.http
                 .post(this.Url, body.toString(), options)
                 .toPromise()
                 .then(this.extractData)
                 .catch(this.handleError);

         private buildLoginUserPasswordRequest(password:string, key:string): any {

        var m_dataRequest = {
                    "ser:nativeAppAuth": {
                        "-xmlns:ser": "http://services.mobileappbc.ws.todo1.com/",
                        "password": this.utilService.buidRSAPass(password, t1Assertion),
                        "key": key,
                        "deviceInfo": this.utilService.getDeviceInfo()
                    }
                };
        return m_dataRequest;
     }

The Content-type is application/x-www-form-urlencoded because the backend need the info of this way. My problem is the character ":" is not convert to equivalent urlencoded %3A+. this cause a problem in my backend service.

any Suggestion for solve this? Thanks!

Upvotes: 1

Views: 8515

Answers (1)

misha130
misha130

Reputation: 5726

Json.stringify does not uri encode data because it can handle it. You need to use uriencode() https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURI

Upvotes: 1

Related Questions