Angular 6 htttp POST - setting params as query string in URL
11,059
We have to pass params as 3rd parameter for post request
public findProduct(productCode: string) {
const url_ = 'api/product/findProduct';
const params = new URLSearchParams();
params.set('productCode', productCode);
return this.http.post(url_,,params, httpHeaders)
.subscribe(
result => {
console.log(result);
},
error => {
console.log('There was an error: ')
}
);
Related videos on Youtube
Author by
Wojciech
Updated on June 04, 2022Comments
-
Wojciech almost 2 years
I have an applicataion based on asp .net core boilerplate. I need to send http POST parameters from my Angular 6 app.
My service looks like this:
public findProduct(productCode: string) { const url_ = 'api/product/findProduct'; const params = new URLSearchParams(); params.set('productCode', productCode); return this.http.post(url_, params, httpHeaders) .subscribe( result => { console.log(result); }, error => { console.log('There was an error: ') } );
I have imported URLSearchParams from @angular/http, but still I have the same problem. My POST URL is bad, because API expect POST URL like this: http://localhost:21021/api/product/findProduct?productCode=1111 and query string parameters like: productCode: 1111
but mine looks like this: http://localhost:21021/api/product/findProduct and setting request payload (always empty):
**{rawParams: "", queryEncoder: {}, paramsMap: {}} paramsMap: {} queryEncoder: {} rawParams: ""**
My httpHeaders is: 'Content-Type': 'application/json', 'Accept': 'text/plain'
My question is how can I set expected API POST parameters in this service?
-
Patricio Vargas over 5 yearsYour problem or one of your problem is that you have ulr_, and url_ you never specify your server where are you specifying the localhost?
-
Wojciech over 5 yearsIt's my missing in this post. But thanks ;)
-
Patricio Vargas over 5 yearsalso correct me if I'm wrong but it seems you are passing an empty const params
-
Patricio Vargas over 5 yearsAre you sure this is a post? sounds like a get to me but i could be wrong
-
Wojciech over 5 yearsIt's example only. I am assing params. And, yes it's POST. I know that it'll be GET, but I'm not backend developer :)
-
Patricio Vargas over 5 yearsI don't think it's possible to do what you want. The "params" are not the params to be passed as a part of the URL string like you are thinking, that for GET only. Take a look to the difference between post and get. diffen.com/difference/GET-vs-POST-HTTP-Requests
-
Wojciech over 5 yearsBut swagger can do it.. I don't know how.. swagger.io
-
Patricio Vargas over 5 yearshaven't heard of it. So if you can do it using swagger only or other tool ONLY that means is not native to http
-