Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encode query params containing special characters in Angular 7?

I am trying to encode query parameter values in Angular 7. But I get 400 Bad Request status code, when my query parameter values contain any special character

Failed Request URL: http://localhost/api/67/cardvalues?parentcardvalues=STONE&parentcardvalues=STONE%20AGE

However, I get 200 OK status code , when my query parameter values do not contain any special characters.

Successful Request URL: http://localhost/api/67/cardvalues?parentcardvalues=STONE&parentcardvalues=STONE

The expected request url has to be like http://localhost/cardvalues?parentcardvalues=VALUE1&parentcardvalues=VALUE2

Below is my code,

parentcardvalues=["STONE","STONE AGE"]

let myparams = new HttpParams();

    if(parentcardvalues.length != 0)
       parentcardvalues.forEach((value) => { 

myparams = myparams.append( 'parentcardvalues',   encodeURIComponent(value) );

});

 this.http.get(this.baseUrl + 'api/67/cardvalues', {params: myparams});

The Swagger specification is,

Curl

curl -X GET "http://localhost/api/cardvalues?parentcardvalues=STONE&parentcardvalues=STONE%20AGE" -H  "accept: application/json"

Request URL

http://localhost/api/cardvalues?parentcardvalues=STONE&parentcardvalues=STONE%20AGE
like image 658
jrapose Avatar asked Apr 04 '19 21:04

jrapose


1 Answers

you can decode your params using:

decodeURIComponent(encodedURI_string)

more information here

like image 99
Muhammad Abdullah Shafiq Avatar answered Sep 22 '22 10:09

Muhammad Abdullah Shafiq