1

I have a get request that requires passing some data, how do I do that in my service:

private campaignUrl = environment.apiUrl + '/campaign/';

    constructor(private http: HttpRequestService) { }

    getCampaign(uuid) {
        return this.http.post(this.campaignUrl);
    }

I need to pass the id after campaignUrl, and this is my api url in swagger:

/api/campaign/{uuid}/

2 Answers 2

3

Is a get or is a post?

anyway you can do it this way :

return this.http.post(this.campaignUrl + uuid);
Sign up to request clarification or add additional context in comments.

Comments

2

The other answer is adequate, but I'll offer this since you were almost there with your example, using ES6 template literals (back-ticks):

private campaignUrl = environment.apiUrl + '/campaign/';

constructor(private http: HttpRequestService) { }

getCampaign(uuid) {
    return this.http.get(`${this.campaignUrl}/${encodeURI(uuid)}`);
}

Or with arrows

getCampaign = (uuid) => this.http.get(`${this.campaignUrl}/${encodeURI(uuid)}`);

Note that you probably want to call encodeURI() (since Angular won't do that for you). You could skip this if you can guarantee that the string passed to getCampaign is always a uuid format (nnnn-nnnn-...).

Also, you say "get request" in your question but call .post, so I assumed your text was correct and code was not.

2 Comments

Yep, that's a better way to concatenate string. You're right.
@JacopoSciampi Your answer is just fine and maintains the original style of the question. I only added this "for completeness".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.