1

I started typing and http.get accepts only url so i don't have any idea how to send and id of a person to get that person from my api which gets it from Sql database .

public getByID(n :number) : Promise<any>{

    return this.http.get(this.url).toPromise();
  }

maybe appending it to an url ?

4 Answers4

6

You can optionally use template strings to add the parameter to the URL

return this.http.get(`${this.url}?id=${n}`).toPromise();
Michael Doye
  • 8,063
  • 5
  • 40
  • 56
  • Yep it works , but could you explain what actually happens in here `${this.url}?id=${n}` is it some kind of string builder by TypeScript or i'm wrong ? – Алекса Јевтић Nov 28 '17 at 12:26
  • 1
    sure its called string interpolation: "...when you want to generate some string out of some static strings + some variables. For this you would need some templating logic and this is where template strings get their name from" (from the link above) – Michael Doye Nov 28 '17 at 12:29
  • thanks alot for that content about template strings , and one more thing does this ? have any functionality or it's just a question mark making the url look like this http.example.com?id=1 or the url that my api will get will look different then i wrote it ? – Алекса Јевтић Nov 28 '17 at 12:34
  • It essentially tells the browser that what comes after the `?` should be interpreted as query string parameters - You can read some info here - https://en.wikipedia.org/wiki/Query_string – Michael Doye Nov 28 '17 at 12:37
  • 1
    yea i get it now , i'm sending the query string after the url in type TKey=TValue , thank you . – Алекса Јевтић Nov 28 '17 at 12:40
2

If you want to pass variables to the API using GET that would be possible using query string. Remember to escape (urlencode) them properly! It is also possible to use POST, if you dont want your variables to be visible.

Manually formatting the query string is fine for simple situations. But it can become tedious when there are many parameters.

You could write a simple utility function that handles building the query formatting for you.

function formatParams( params ){
return "?" + Object
    .keys(params)
    .map(function(key){
        return key+"="+encodeURIComponent(params[key])
    })
    .join("&")

}

And you would use it this way to build a request.

var endpoint = "https://api.example.com/endpoint"
var params = {
a: 1,
b: 2,
c: 3
}


return this.http.get(this.url+ formatParams(params)).toPromise();
vijayliebe
  • 158
  • 6
1

Here is an example of what you would need to do

public getByID(n :number) : Promise<any>{  
    return this.http.get(this.url + '?id=' + number).toPromise();
}
Wesley Coetzee
  • 4,768
  • 3
  • 27
  • 45
0

If you want to pass id you can just make post request. It would like return this.http.post(this.url, {id: n}).toPromise(), second argument is just body of request, it can be an array of object, or just object. Then on the server side you just have to parse it to JSON format. You can also use get method, but then you have to send it as search url param. Take a look at this here

Patryk Brejdak
  • 1,571
  • 14
  • 26