Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 htttp POST - setting params as query string in URL

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?

like image 781
Wojciech Avatar asked Jun 03 '26 20:06

Wojciech


1 Answers

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: ')
        }
      );
like image 114
Sneha Avatar answered Jun 05 '26 11:06

Sneha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!