HTTP get url with spaces in between

If i send HTTP get with spaces in url i am getting 400 error if i send it without spaces it return success.
Have a look below.,

http://151.11.1./ContactId=1113&UserId=1037&FirstName=Vaishnavishri ss&Lastname=dall dall ->This is getting 400 error.

http://151.11.1./ContactId=1113&UserId=1037&FirstName=Vaishnavishriss&Lastname=dalldall ->This is getting success.

You need to URLencode it before making the call.

See:

3 Likes

Replace the spaces with “+”. This is urlencoded for that space.

Another option would be to use Http's native query string building feature:

let qp = new URLSearchParams();
qp.set('UserId', '1037');
qp.set('FirstName', 'Vaishnavishri ss');
qp.set('LastName' 'dall dall');
http.get('api url', {search: qp});

Http should do all the necessary escaping for you.

1 Like

i have tried this and its not working.

qp.set('UserId', this.userId);

however this work

qp.set('UserId', '23');

how do I use a variable for that

This is vague and conveys little useful information. Are you losing execution context? Does the word “function” appear anywhere in your code?