Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a different Http Status in loopback 4

I can't find any ressources on how to change the success HTTP code using loopback 4.

For example :

201 "created" on post method

204 "no content" on delete method

I tried to specify this in the @api decorator but this change is not reflected in the actual response.

Thank's for your help !

like image 822
Felix Movee Avatar asked Feb 12 '19 17:02

Felix Movee


People also ask

How do I enable CORS in LoopBack?

LoopBack applications use the cors middleware package for cross-origin resource sharing (CORS), but it is disabled by default for applications created with the application generator. To enable CORS, ensure that remoting. cors is set to false in server/config. json .

What is loopback4?

LoopBack is a Node. js API framework that enables you to create APIs quickly that interact with backend resources like databases and services. LoopBack 4, the next generation of LoopBack, includes: A brand new core rewritten in TypeScript that makes this framework simpler to use and easier to extend than ever.

What is LoopBack Explorer?

About LoopBack built-in models In addition to the CoffeeShop model that you defined, by default Loopback generates the User model and its endpoints for every application. LoopBack creates several other models for common use cases. For more information, see built-in models .


1 Answers

I can't find any ressources on how to change the success HTTP code using loopback 4.

We don't have first-class support for this feature yet. The current workaround is to inject the Response object into your controller method and set the status code directly via Express/Node.js core API.

export class TodoController {
  constructor(
    @repository(TodoRepository) protected todoRepo: TodoRepository,
    @inject(RestBindings.Http.RESPONSE) protected response: Response,
  ) {}

  async createTodo(@requestBody() todo: Todo): Promise<Todo> {
    this.response.status(401);
    // ...
  }
}

Don't forget to import Response and RestBindings from @loopback/rest, and inject from @loopback/core. Add the below imports in your controller.

import { Response, RestBindings } from '@loopback/rest';
import { inject } from '@loopback/core';

201 "created" on post method

See the discussion in https://github.com/strongloop/loopback-next/issues/788. The difficult part is how to figure out what URL to send in the Location response header.

204 "no content" on delete method

Just change your controller method to return undefined instead of the current {count: 1} object. I believe this is the default behavior for CRUD controllers scaffolded by our lb4 tool.

export class TodoController {
  // ...
  @del('/todos/{id}', {
    responses: {
      '204': {
        description: 'Todo DELETE success',
      },
    },
  })
  async deleteTodo(@param.path.number('id') id: number): Promise<void> {
    await this.todoRepo.deleteById(id);
  }
like image 198
Miroslav Bajtoš Avatar answered Sep 23 '22 21:09

Miroslav Bajtoš