question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

URL query string value encoding

See original GitHub issue

🐞 bug report

Affected Package

The issue is caused by package @angular/....

@angular/common/http

Is this a regression?

Yes, the previous version in which this bug was not present was: ....

I am not sure, I did not check.

Description

A clear and concise description of the problem...

If we pass data in query string in GET method with param value of HTTP PARAMS, by default it is not converting β€˜@’ symbol to %40 or any other reserved symbol with is corresponding UTF-8 code. It should convert β€˜@’ to %40, because of this I am not able to use http get with querystring. eg, http://localhost:4200/api/usersData?userName=xxx.xxx@xxx.com&password=xxx@xxx above request is failing.

Expected / new behavior:

function standardEncoding(v: string): string { return encodeURIComponent(v) .replace(/%40/gi, β€˜@’) .replace(/%3A/gi, 'πŸ˜‚ .replace(/%24/gi, β€˜$’) .replace(/%2C/gi, β€˜,’) .replace(/%3B/gi, β€˜;’) .replace(/%2B/gi, β€˜+’) .replace(/%3D/gi, β€˜=’) .replace(/%3F/gi, β€˜?’) .replace(/%2F/gi, β€˜/’); }

this code is not able to convert @ to %40. It should do that. If is write replace(β€˜@’, β€˜%40’) then it is working.

Minimal reproduction of the problem with instructions:

Below mentioned code is the service code which is making a GET request with query string.

data passed is, userName = xxx@xxx.com password = pwd@#

const options = { headers: new HttpHeaders({ β€˜Content-Type’: β€˜application/json’, }), observe: β€˜response’ as β€˜body’, params: null, reportProgress: true, responseType: β€˜json’ as β€˜json’, withCredentials: true };

@Injectable({ providedIn: β€˜root’ }) export class AuthenticationService {

constructor(private http: HttpClient) { }

// login user login(requestParam: { userName: string, password: string }): Observable<HttpResponse> { options.headers = options.headers.set(β€˜Content-Type’, β€˜application/x-www-form-urlencoded’); options.params = new HttpParams({ fromObject: requestParam, encoder: this.httpParameterCodec }); console.log(options.params); return this.http.get<HttpResponse<Observable>> (${environment.API_URL}usersData, options) .pipe( // retry(3), catchError(this.handleError) ); }

πŸ”¬ Minimal Reproduction

https://stackblitz.com/...

I am using In-Memory-Api for processing the report, then I am sent the queryString like ?userName=xxx@xxx.com&password=xxxx@xx @, $ etc are not processing the request.

πŸ”₯ Exception or Error





http://localhost:4200/api/usersData?userName=bc@abc&password=abc 404 (Not Found) Server error respose code: 404 Body was: <!DOCTYPE html>

<html lang="en"> <head> <meta charset="utf-8"> <title>Error</title> </head> <body>
Cannot GET /api/usersData
</body> </html>

🌍 Your Environment

Angular Version:





Angular CLI: 7.3.8 Node: 10.15.3 OS: win32 x64 Angular: 7.2.13 … animations, common, compiler, compiler-cli, core, forms … language-service, platform-browser, platform-browser-dynamic … router

Package Version

@angular-devkit/architect 0.13.8 @angular-devkit/build-angular 0.13.8 @angular-devkit/build-optimizer 0.13.8 @angular-devkit/build-webpack 0.13.8 @angular-devkit/core 7.3.8 @angular-devkit/schematics 7.3.8 @angular/cli 7.3.8 @ngtools/webpack 7.3.8 @schematics/angular 7.3.8 @schematics/update 0.13.8 rxjs 6.3.3 typescript 3.2.4 webpack 4.29.0

Anything else relevant?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
spinnercatcommented, Jun 26, 2019

Why does the default HTTP encoder use standardEncoding (which does not encode special characters like ;) to encode keys and values instead of just encodeURIComponent? If you have an object that is {"key": "value;with+characters"} shoudn’t this be encoded to key=value%3Bwith%2Bcharacters as opposed to key=value;with+characters, as the latter seems very likely to be misinterpreted by the web server when it tries to parse the query string? I get that for encoding URIs in general, the semicolon is an acceptable character and should not be encoded, hence the existence of standardEncoding but when encoding keys and values of params, you clearly don’t want to have special characters left alone that might affect how the query string gets parsed…

EDIT: I guess my question is this issue https://github.com/angular/angular/issues/18261

1reaction
arobinsoncommented, May 11, 2019

I’m also seeing encoding issues on Angular 7.2.5:

const fileName = 'Payment!@#$%^()_+&Terms.csv'
const correctEncoding = encodeURIComponent('Payment!@#$%^()_+&Terms.csv');
const angularEncoding = (new HttpParams()).encoder.encodeKey(fileName);
console.log('Correct: %o,\nAngular: %o\nEqual:%o', correctEncoding, angularEncoding, correctEncoding === angularEncoding);

Output:

Correct: "Payment!%40%23%24%25%5E()_%2B%26Terms.csv",
Angular: "Payment!@%23$%25%5E()_+%26Terms.csv"
Equal:false

This is preventing us from downloading some files from our backend due to bad URL escaping

Read more comments on GitHub >

github_iconTop Results From Across the Web

Query string - Wikipedia
URL encoding Edit Β· Characters that cannot be converted to the correct charset are replaced with HTML numeric character references Β· SPACE is...
Read more >
Should I url encode a query string parameter that's a URL?
RFC 2396 sec. 2.2 says that you should URL-encode those symbols anywhere where they're not used for their explicit meanings; i.e. you should...
Read more >
URL Encode Decode - URL Percent Encoding and Decoding.
URL encoding stands for encoding certain characters in a URL by replacing them with one or more character triplets that consist of the...
Read more >
Online URL Query String encode tool - CodersTool
This is a simple tool that will help you encode text that can be safely put into URL query string values known as...
Read more >
Javascript | Encode URL Query Parameter | by Sonika - Medium
URLs can only have certain characters from the standard 128 character ASCII set. Reserved characters that do not belong to this set must...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found