2

How to make get request by passing parameters? I tried this but getting 404 error

getOrderbyOrderNo() {

        this.orderno = this.cookie_service.get('key');

        let headers = new Headers();
        headers.append('Content-Type', 'application/json');
        headers.append('orderno', this.orderno );
        let params = new URLSearchParams();
        params.append("someParamKey", this.orderno )

        return this.http.get('http://localhost:8080/View/orders', { headers: headers, params: params })


    }

output

ERROR 
Object { _body: "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>Error</title>\n</head>\n<body>\n<pre>Cannot GET /View/orders</pre>\n</body>\n</html>\n", status: 404, ok: false, statusText: "Not Found", headers: {…}, type: 2, url: "http://localhost:8080/View/orders" }
3
  • you are using httpclient or http ? Commented May 9, 2018 at 6:53
  • show your serverside method also ...if it working fine then do change as given in answer Commented May 9, 2018 at 7:08
  • is that worked for you ?? Commented May 9, 2018 at 8:25

5 Answers 5

1

IMO there are two ways by which you can send params with http request.

  1. By appending it to URL like this -

    return this.http.get('http://localhost:8080/View/orders/'+this.orderno, { headers: headers  })
    
  2. Another way is, If you are using httpclient then you can send params in the options like this-

     let httpParams = new HttpParams()
        .set('orderno', this.orderno);
    

For more about this read out here

In get type of request there is no body part of request so you have to append your params in your URL i.e params or query params. or using second way in options.

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

1 Comment

What's wrong with this answer may i KNow? why Downvote?
1

if you are using httpclient then you should do this

let params = new HttpParams();
params = Params.append('firstParameter', parameters.valueOne);
params = Params.append('secondParameter', parameters.valueTwo);
return this._HttpClient.get(`${API_URL}/api/logs`, { params: params })

or you can create object of params

export interface GetParams {
  firstParameter?: string;
  secondParameter?: string;
   ..more param you want 
} 


Const getParamsobj : GetParams = {
  firstParameter: 'value',
  secondParameter: 'value'
  ..other params
}
return this._HttpClient.get(`${API_URL}/api/logs`, { params: getParamsobj })

10 Comments

where does this params will add at the time of httpCall ?
@PardeepJain - not getting you
I mean to say is your method is different from my? or after exceution it will append with URL?
@PardeepJain - it will append parameter to url, so no need to do manually as you did
so Basically OUR answers are same, with different method :)
|
0

you can pass parameters like this:

return this.http.get('http://localhost:8080/View/orders?someParamKey=xyz&anotherParam=abc
', { headers: headers})

Comments

0

Passing parameters and headers is different in HttpClient since they're immutable, If you want to pass the parameter you should use HttpParams. { params: new HttpParams().set('name', term) }

Comments

0

Try this:

import this packages to your class:

import { HttpHeaders, HttpClient, HttpParams } from '@angular/common/http';

inject HttpClient like this:

constructor(private _http: HttpClient) {}

send request:

    let url = "your url";

    let Params = new HttpParams();
    Params = Params.append('param1', value1);
    Params = Params.append('param2', value2);

    let header: HttpHeaders = new HttpHeaders();
    header = header.append('Content-Type', 'text/plain');

    return this._http.get(url , { params: Params, headers: header })
        .map((response: Response) => {
        });

2 Comments

@rina what is the problem ? why un accept my answer?
parameters are passing but showing all table data, it's not appending with the url

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.