1

I have a problem with plus (+) in Angular 9 that is replaced with space. The use case is as following:

  1. I'm clicking on a link:

    https://myapp.com/invitation?code=h7d8wdh7ddjwkdfhg93&email=alan.jack+432@gmail.com

  2. On the frontend we are intercepting the request:

@Component({
  selector: '-my-app',
  templateUrl: ...
  styleUrls: ...,
  changeDetection: ChangeDetectionStrategy.OnPush
})
    export class MyAppComponent {
      constructor(private activatedRoute: ActivatedRoute,
                  private router: Router) {
        this.watchForInvitation();
      }
      private watchForInvitation() {
        this.activatedRoute.queryParams.pipe(
          filter(params => params.code && params.email),
          switchMap(params => {
            const code = params.code;
            const email = params.email;
    
            const body: UserInfo = {
              emailAddress: email, //email without + -> how to prevent the plus sign from being removed?
            };
            return this.usersHttpService.accept(code, body); //body should be with plus and not with space
          }),
        ).subscribe(_ => {
        ...
        }, err => {
        ...
        });
      }
    }

Below the accept method from service:

usersHttpService:
    accept(code: string, user: UserInfo) {
        return this.http.post(`${this.env.MY URL}/mon/accepting/${code}/accept`, user);
      }

How to prevent the plus sign from being removed in my example? Frontend receives the correct URL with + but then the plus sign is somehow removed.

Matley
  • 1,953
  • 4
  • 35
  • 73
  • Does this answer your question? [Angular url plus sign converting to space](https://stackoverflow.com/questions/45428842/angular-url-plus-sign-converting-to-space) – sloth Aug 17 '20 at 11:14
  • I wonder if I can just replace space with + like const email = params.email.replace(' ', '+'); I think in my case it should be the simplest way to fix my problem, Am I right? – Matley Aug 17 '20 at 11:20
  • 1
    The system generating the URL makes a mistake by not URL-encoding the parameter values. Any measure you take on the receiving side will make the problem worse (i.e. generating more special cases that go wrong) – Henry Aug 17 '20 at 11:49

0 Answers0