1

I couldn't find what I was looking for and therefore wanted to ask how to send a request to a JSON api with parameters.

The important thing is that the parameters need to be encoded. The query will contain a string which could contain any characters (+, &, " ...). So it is important that it doesn't interfere with the query string.

It would like so: (Just with the query included)

    this.http.get('https://abcdefg.com/setting').map(res => res.json()).subscribe(data => {
      console.log(data);
    });

The Query parameters could be:

ssid: WiFi-Network pass: &aBc+1234567890

I would appreciate a solution.

CrypticPug

3
  • 1
    developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… ? Commented Jun 10, 2017 at 10:38
  • @Maximus Thanks. That helped. Now I can just put the query parameters just into the URL String without any more changes. Super! Commented Jun 10, 2017 at 12:03
  • you're welcome, I can post it as answer if helped Commented Jun 10, 2017 at 12:04

1 Answer 1

2

Thanks to Maximus, I found the solution.

Instead of dealing with parameter objects and so on I just did this:

    this.http.get('https://your-url.com/?key1=' + encodeURIComponent(value1) + '&key2=' + encodeURIComponent(value2) ).map(res => 

    });

That's all you need. The Parameters are encoded, so there is no problem with & or + in the query.

.

Thanks!

Cryptic Pug

Sign up to request clarification or add additional context in comments.

1 Comment

You could also use URLSearchParams which is available, so that you do not need to manually build the url. You can well use encodeURIComponent with that :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.