Reputation: 85
while following a tutorial I've come across an error while I was trying to post data to api.
The error I got:
Failed to load resource: the server responded with a status of 422 (Unprocessable Entity)
code of service.Js file
import 'rxjs/add/operator/toPromise';
import {Injectable} from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {CONFIG} from './../config/config';
import { RequestOptions, Headers, } from '@angular/http';
import { headersToString } from '../../../node_modules/@types/selenium-webdriver/http';
@Injectable()
export class AuthService {
private _options = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };
constructor(private http: HttpClient) {}
register(name: string, email: string, password: string) {
const headers: Headers = new Headers({ 'Content-Type': 'application/json' });
return this.http.post(`${CONFIG.API_URL}/register`, { name: name, email: email, password: password}, this._options)
.toPromise()
.then((response) => {
console.log(response);
});
}
}
I'm unable to find any solution of this problem so any help would be appreciated.
Upvotes: 1
Views: 4773
Reputation: 10730
Seems more of a server side problem rather than client code problem.
Acc to defination of 422 : The server understands the content type of the request entity (hence a 415 Unsupported Media Type status code is inappropriate), and the syntax of the request entity is correct (thus a 400 Bad Request status code is inappropriate) but was unable to process the contained instructions.
Check network pannel to verify the if correctly formatted JSON is passed and proper headers are set.
Try to debug the problem at server end by checking exceptions and debugging the flow.
Also show the JSON generated that you are passing to server to get the exact problem & mention the back-end framework.
Upvotes: 1