HTTP POST request with x-www-form-urlencoded in Agular 6

Asked

Viewed 756 times

0

I need to make an HTTP POST request with x-www-form-urelcoded at angle 6.

I tried something like:

  pesquisaEmail(email): Observable<any>{
    let headers = new HttpHeaders();
    headers.append('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    headers.append('Content-Type', 'application/x-www-form-urlencoded');

    this.token = localStorage.getItem('currentUser');

    return this._http.post<any>(AppSettings.API_ENDPOINT_EMAIL,
      {email: email,
      token_jc: this.token},
      { headers: headers, observe: 'response' })
        .pipe(
          map((response) => ({data: response.body, status: response.status}))
      )}

}

@Edit I also tried to:

pesquisaEmail(email): Observable<any>{
    let headers = new HttpHeaders();
    headers.append('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    headers.append('Content-Type', 'application/x-www-form-urlencoded');

    this.token = localStorage.getItem('currentUser');
    let body = new URLSearchParams();
    body.set('email', this.token);
    body.set('token_jc', email);

    return this._http.post<any>(AppSettings.API_ENDPOINT_EMAIL,
      body,
      { headers: headers, observe: 'response' })
        .pipe(
          map((response) => ({data: response.body, status: response.status}))
      )}

}

but without success

I get a bad request error. Does anyone know how I can make this kind of requisition?

  • here is an example:https://gist.github.com/dherges/442d3a7bc65e3e46d8dead728e4d8be8

  • What exactly is the mistake?

  • I tried to do the http post by Postman and get the data I need, but returns 400 Bad Request. It is possible to capture this data even having a bad request or the problem is in the backend?

  • I think the problem is in the backend!

  • By Postman I receive as return 400 but at the same time returns the data I need. Ai to without knowing what to do.

  • So there’s an error screen for you to add to your question?

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.