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 232 forks source link

GET is ok, but POST doesn't work with the same url #171

Open pierre03 opened 6 years ago

pierre03 commented 6 years ago

Hello, I use this library for my test, it works very well, so thank you very much for this pretty job. Recently i have a problem with the POST request, the GET method works, but when i change the method to POST with the same api name, i have an error: ERROR: Error: [object Object] at viewWrappedDebugError... My config is:
{dataEncapsulation: false, passThruUnknownUrl: true} Can you tell me how to fix it, please?

pierre03 commented 6 years ago

Someone here?

opirci commented 6 years ago

I use this lib and did not have a problem with POST. May be you should try again after updating with the latest version. If it doesn't fix can you share the code?

kuncevic commented 6 years ago

Was getting same error having single object :

{
  accessToken: `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...`,
};

had to change to collection with an 'id' field:


 public static accessData = [
    {
      id: 1, // need to have 'id' field
      accessToken: `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...,
    }
  ];

Now post does work but it is returning a new record which is wrong for my case. I have kinda login from so no really need to insert anyting

UPDATE: Looks like in case of custom post where you do not need to insert anything you need to implement somethng like this https://github.com/angular/in-memory-web-api/blob/master/src/app/hero-in-mem-data-override.service.ts#L50 which is custom ingerceptor. So that is some code I come up with:

  // HTTP GET interceptor
  post(reqInfo: RequestInfo) {
    const collectionName = reqInfo.collectionName;
    if (collectionName === 'token') {
      return this.getResponse(reqInfo, TokenFakeDb.accessData);
    }
    return undefined; // let the default GET handle all others
  }

  private getResponse(reqInfo: RequestInfo, collection: any) {
    return reqInfo.utils.createResponse$(() => {
      console.log('HTTP GET override');

      const dataEncapsulation = reqInfo.utils.getConfig().dataEncapsulation;
      const id = reqInfo.id;

      // tslint:disable-next-line:triple-equals
      const data = id == undefined ? collection : reqInfo.utils.findById(collection, id);

      const options: ResponseOptions = data ?
        {
          body: dataEncapsulation ? { data } : data,
          status: STATUS.OK
        } :
        {
          body: { error: `'Item' with id='${id}' not found` },
          status: STATUS.NOT_FOUND
        };
      return this.finishOptions(options, reqInfo);
    });
  }

  private finishOptions(options: ResponseOptions, {headers, url}: RequestInfo) {
    options.statusText = getStatusText(options.status);
    options.headers = headers;
    options.url = url;
    return options;
  }

and now I can have only one item without having a collection with item id's:

{
  accessToken: `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...`,
};