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:

4 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?

For issues using Ionic Native HTTP or others ,try like this to fill spaces in http url spaces to fill with %20(space) incase of url breaks after spaces.

url = “http://xyz.com?name=foo bar”

var newUrl = encodeURI(url)

result is => http://xyz.com?name=foo%20bar