0
votes

I am working on Angular 8

I am trying to send data to server as HttpParams but getting 502 error

HttpErrorResponse {headers: HttpHeaders, status: 502, statusText: "Bad Gateway"

In browser's Network's Header i can see that data is going as formData

I am sharing my code

service file

userlogin(userid, smpassword) {

const params = new HttpParams()
.set('user_id', userid)
.set('sm_password', smpassword);

console.log(params);


return this.http.post(this.baseUrl + 'user/login', params);

  }

component

 login() {

    const email =  this.Userlogin.get('email').value;
    const password = this.Userlogin.get('password').value;
    this.rest.userlogin(email, password).subscribe(
      result => {
        console.log(result);
      }
    );

    //  this.route.navigateByUrl('/pendingapproval');

  }
2
An HTTP request/response body that represents serialized parameters, per the MIME type application/x-www-form-urlencoded.enno.void
@enno.void how i will write typeAnurag Ranjan
@enno.void body.toString(), { headers: new HttpHeaders() .set('Content-Type', 'application/x-www-form-urlencoded') }Anurag Ranjan

2 Answers

2
votes

Note that on the documentation the post method suppose that you provide posted object in the second parameter.

  post(url: string, body: any, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: HttpObserve; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "arraybuffer" | "blob" | "text" | "json"; withCredentials?: boolean; } = {}): Observable<any>

So when you are sending your parameters it's actually interpreted as the posted object

try with a dummy object

this.http.post(this.baseUrl + 'user/login', mydummyObject, params);

Or even better try with this.http.get since you do not have any object to post!!

0
votes

use this

let params = new HttpParams();
params = params.append('user_id', userid);
params = params.append('sm_password', smpassword);
this.http.post(this.baseUrl + 'user/login', {params:params });