Reputation: 127
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" }
Upvotes: 2
Views: 10120
Reputation: 86730
IMO there are two ways by which you can send params with http request.
By appending it to URL like this -
return this.http.get('http://localhost:8080/View/orders/'+this.orderno, { headers: headers })
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.
Upvotes: 1
Reputation: 6086
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) => {
});
Upvotes: 0
Reputation: 176896
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 })
Upvotes: 1
Reputation: 76
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) }
Upvotes: 0
Reputation: 1006
you can pass parameters like this:
return this.http.get('http://localhost:8080/View/orders?someParamKey=xyz&anotherParam=abc
', { headers: headers})
Upvotes: 0