1
votes

My API accept this data and tried in a postman its working fine :

http://{URL}/password/email

Method : POST

Header : [{"key":"Accept","value":"application/json","description":""} , {"key":"Content-Type","value":"application/x-www-form- urlencoded","description":""}]

Body : only working if I send key:value in x-www-form-urlencoded.

my IONIC 2 code is :

return new Promise((resolve, reject) => {
       let headers = new Headers();
       headers.append('Accept', 'application/json');
       headers.append('Content-Type', 'application/x-www-form-urlencoded');
        let body = new FormData();
        body.append('email', email);
        let options = new RequestOptions({ headers: headers });

        this.http.post(config.API_URL+"/password/email" ,body , options)
          .subscribe(res => {
            resolve(res.json());
          }, (err) => {
            reject(err.json());
          });
    }); 

and I am getting error

POST {URL}/password/email 422 (Unprocessable Entity)  polyfills.js:3 

I don't know what's wrong with my code?

2
have you tried using JSON.stringify(body) in this.http.post(config.API_URL+"/password/email" ,JSON.stringify(body) , options) - Niladri
Yes.. still the same error 422 - Harpreet Singh

2 Answers

0
votes

Try the below code, where did the emailId is coming from? if it's gloabl variable use this.emailId.

The Content-Type header in an HTTP request or response describes the content type for the message body.

The Accept header in the request tells the server the content types that the client is expecting in the response body

Please refer the API for the type of data that's expecting and the type of data that it will send.

    return new Promise((resolve, reject) => {
           let headers = new Headers();
           headers.append('Content-Type', 'application/json');
            let body = JSON.stringify(
          {
            "emailId": this.emailId
          }
        );

            let options = new RequestOptions({ headers: headers });

            this.http.post(config.API_URL+"/password/email" ,body , options)
              .subscribe(res => {
                resolve(res.json());
              }, (err) => {
                reject(err.json());
              });
        }); 
0
votes

I had the same problem with Laravel APIs then it can be 2 kinds of problem.

  • Your Authorization token is missing. Try add Authorization on the headers:

    let headers = new Headers(); headers.append('Accept', 'application/json'); headers.append('Content-Type', 'application/x-www-form-urlencoded'); // adding Authorization token in your headers headers.append("Authorization:", token);

  • You're forgetting send some field or object on your request. Take a little time to read this code descriptions

Code ... Description

200 ...... OK
400 ...... Inverse Request. The request is invalid in general.
401 ...... Unauthorized. The user and password or access token are invalid.
403 ...... Forbidden. API access is blocked or the user is blocked.
404 ...... not found. The access address does not exist.
422 ...... Non-Processable Entity. The request is valid, but the data passed is not valid.
429 ...... Many requests. The user has reached the request limit.
500 ...... Internal Server. Error An internal server error occurred while processing a request