angular / in-memory-web-api

The code for this project has moved to the angular/angular repo. This repo is now archived.
MIT License
1.18k stars 230 forks source link

Status 422 for http post in Angular 6 #216

Open angelitoTalens opened 6 years ago

angelitoTalens commented 6 years ago

Encountered this error: body:error:"Collection 'login' id type is non-numeric or unknown. Can only generate numeric ids." status:422 statusText:"Unprocessable Entity"

when doing a post using:

const options = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) }; const body = JSON.stringify({email: email, password: password}); return this.http.post<boolean>(apiUrl, body, options);

chorsnell commented 5 years ago

@angelitoTalens did you get to the bottom of this?

hungerregnuh commented 5 years ago

This is working fine for me in v7, how did you setup your in memory data service?

angelitoTalens commented 5 years ago

@hungerregnuh

mock-api.service.ts

export class MockApiService implements InMemoryDbService { createDb() { let login:{email:string, password:string}[] = [{ email:'first.last@gmail.com', password:'pwd' }];

return { login }; } }

In app.module.ts

imports: [ ... environment.production ? [] : HttpClientInMemoryWebApiModule.forRoot(MockApiService, {post204: false, post409: true, dataEncapsulation: false}) ],

hungerregnuh commented 5 years ago

@angelitoTalens

could you try this instead?

createDb() {
  const login = [
    {
      id: 1,
      email: 'first.last@gmail.com',
      password: 'pwd'
    }
  ];
angelitoTalens commented 5 years ago

@hungerregnuh

your suggestion worked for me.

let userCredentials = {email: email, password: password}; return this.http.post<boolean>(apiUrl, userCredentials, options)

keerthi42 commented 5 years ago

Encountered this error: body:error:"Collection 'inputparams' id type is non-numeric or unknown. Can only generate numeric ids." status:422 statusText:"Unprocessable Entity"

when doing a post using:

Angular 7 angular-in-memory-web-api const url = ${this.apiUrl}; return this.http.post(url, JSON.stringify( param), httpOptions).pipe(map(this.serviceHelper.extractData)) .pipe(catchError(this.serviceHelper.handleError));](url)

here is what the "param" consists of common: OEGenericInputParameter; fleet: OEInputParameterWideBody[]; optional: OEInputParameterOptions;

User gives details in the form all the details are clubbed and made as param model createDB(){ const inputparamsfun = () =>{ what to be given here? } }