Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encode string for URL (angular)

I'm trying to encode a string that's pretty complex, so that I can include it in a mailto:

Component:

<a href="mailto:[email protected]?subject='Hello'&{{body}}"> 

TS:

import { HttpParameterCodec } from "@angular/common/http";  let body = encodeValue('This is the example body\nIt has line breaks and bullets\n\u2022bullet one\n\u2022bullet two\n\u2022bullet three') 

When I try to use encodeValue, I get "cannot find name encodeValue.

How would be best to url-encode body?

like image 434
user749798 Avatar asked Mar 29 '19 15:03

user749798


People also ask

How do I encode a string with URL?

URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

What is decoder in angular?

Output: Decode URL: URL Decoding refers to the process of replacing the “%-based escape sequence” characters with their normal representation in the encoded URLs. URL can easily be encoded or decoded with the help of AngularJS. Given an encoded URL and the task is to decode the encoded URL using AngularJS.

What is the difference between encodeURI and encodeURIComponent?

The difference between encodeURI and encodeURIComponent is encodeURIComponent encodes the entire string, where encodeURI ignores protocol prefix ('http://') and domain name. encodeURIComponent is designed to encode everything, where encodeURI ignores a URL's domain related roots.

What is encodeURIComponent?

The encodeURIComponent() function encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).


2 Answers

HttpParameterCodec : is a codec for encoding and decoding parameters in URLs( Used by HttpParams).

If you need to encode Url you can use the below:

encodeURI assumes that the input is a complete URI that might have some characters which need encoding in it.

encodeURIComponent will encode everything with special meaning, so you use it for components of URIs, such as:

var textSample= "A sentence with symbols & characters that have special meaning?"; var uri = 'http://example.com/foo?hello=' + encodeURIComponent(textSample); 
like image 65
nircraft Avatar answered Sep 21 '22 23:09

nircraft


Both encodeURI() and encodeURIComponent() will work, while there are some differences:

var set1 = ";,/?:@&=+$";  // Reserved Characters  var set2 = "-_.!~*'()";   // Unescaped Characters  var set3 = "#";           // Number Sign  var set4 = "ABC abc 123"; // Alphanumeric Characters + Space    console.log(encodeURI(set1)); // ;,/?:@&=+$  console.log(encodeURI(set2)); // -_.!~*'()  console.log(encodeURI(set3)); // #  console.log(encodeURI(set4)); // ABC%20abc%20123 (the space gets encoded as %20)    console.log(encodeURIComponent(set1)); // %3B%2C%2F%3F%3A%40%26%3D%2B%24  console.log(encodeURIComponent(set2)); // -_.!~*'()  console.log(encodeURIComponent(set3)); // %23  console.log(encodeURIComponent(set4)); // ABC%20abc%20123 (the space gets encoded as %20)

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

like image 33
fishstick Avatar answered Sep 20 '22 23:09

fishstick