Request http post in ionic does not send the data as x-www-form-urlencoded

0

I need to perform an HTTP request to log in to my ionic application, but my function needs to send the data as x-www-form-urlencoded, tried in several different ways but I have not yet been able to do it.

  public login(credenciais):Observable<any>{

    const formData = new FormData()
    formData.append('email', credenciais.email);
    formData.append('password', credenciais.password);

    return this._http.post<any>(AppSettings.API_ENDPOINT + 'loginnovo',
    formData, {observe: 'response'
    }
  )
}

In this way I get:

  

Http failure during parsing

I've also tried:

  public login(credenciais):Observable<any>{


    return this._http.post<any>(AppSettings.API_ENDPOINT + 'loginnovo',
    {email: credenciais.password,
    password: credenciais.password},  
    {observe: 'response'
    }
  )
}

This way I get an Http failure during parsing If I check in Request Payload, I have:

{email: "[email protected]", password: "1234"} email : "[email protected]" password : "1234"

    
asked by anonymous 06.08.2018 / 16:22

2 answers

0

The x-www-form-urlencoded must be entered in the header. Try the following:

public login(credenciais):Observable<any>{

    const formData = new FormData()
    formData.append('email', credenciais.email);
    formData.append('password', credenciais.password);

    let headerJson = {
        'Content-Type': 'x-www-form-urlencoded',
    };
    let headers = new HttpHeaders(headerJson);

    return this._http.post<any>(AppSettings.API_ENDPOINT + 'loginnovo',
        JSON.stringify(formData), {headers: this._headers});

}
    
06.08.2018 / 16:42
0

Folks, I was able to do the request this way:

public login (credentials): Observable {

return this._http.post<any>(AppSettings.API_ENDPOINT + 'loginnovo',
{email: JSON.stringify(credenciais.email), password: credenciais.passowrd}, 
{observe: 'response'})
.pipe(
  map((response) => ({data: response.body, status: response.status}))
)

The problem was in the credentials.email field, so I did JSON.stringify only in the email field and it worked. I do not know why ionic forces me to do this, because in the angular it works normal, anyway, if anyone has any more information about it.

    
06.08.2018 / 19:44