0

I'm trying to send list of ids to the server in a get request, I'm doing the following:

public loadTripsByIds(tripsIds:number[]): Observable<any> {              
    let params = new HttpParams();
    params = params.append('tripsIds', tripsIds.join(', '));    
    return this.http.get<TripObj[]>(`${this.baseUrl}/Trips/ByIds/Get`, {params: params});                        
  }

In the server api code ( rest) I have defined list:

@GET
@Path("Trips/ById/Get") 
@Produces("application/json")
public List<Trips> loadTripsById(@QueryParam("tripsIds") final List<String> tripsIds) {

What I actually getting in the server is a list with 1 item (String type) with comma separated. for example "10001, 10002". I can parse the string in the server side easily but looking for the right way to send list to server where each element will be id.

Thanks.

7
  • 1
    This is purely a back-end question. Please update your question to explain what the back-end is, what Java framework you are using and update the tags for that. You'll get better help than just tagging as Angular. Thanks, Commented Jul 8, 2019 at 7:51
  • 1
    In the backed I have the correct code O think cause i expected list, the problem is the angualr doesn't send the list correctly Commented Jul 8, 2019 at 7:53
  • So: what the loadTripsByIds input arguments are, what does the outgoing request look like, and what is it supposed to look like? Commented Jul 8, 2019 at 7:55
  • 1
    fundsIds.join(', ') create a string which I probably need to change but not sure what to Commented Jul 8, 2019 at 7:56
  • Should work as expected if you just append the array as-is. Parameter serialization should take care of it: params.append('tripsIds', tripsIds) Commented Jul 8, 2019 at 7:57

2 Answers 2

5

In order to solve the problem I;m sending now array of param as follow:

let params = new HttpParams();
    for (let id of tripsIds) {
      params = params.append('tripIds', id); 
    }  
Sign up to request clarification or add additional context in comments.

Comments

1

I am not a Java/Sprint developer, but some back-end frameworks handle duplicate query parameters as a collection. So you need a URL that looks like this.

http://example.com/?id=1&id=2

So try this, but I do not know if this helps.

const params = tripIds.reduce((acc, next) => acc.append('tripsIds', next), new HttpParams());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.