api-gatewaygravitee

Gravitee : How to change request status code


Is it possible to rewrite the status code from a response? My backend service returns a 202, but the requester only accepts 200 as an actual success. Because of that, I want to rewrite the response status code from 202 to 200.

I tried the Mocking policy, but that one will not call the backend services. I also tried the Groovy Policy in the following way:

if(response.status == 202) {
  result.state = State.SUCCESS;
  result.code = 200
  result.error = '{"message":"ok"}'
  result.contentType = 'application/json'
}

But that one also gives me the original 202 http status code.


Solution

  • It is currently a little flaw of the Groovy Policy. To override the status code, you will need to change

    result.state = State.SUCCESS;
    

    to:

    result.state = State.FAILURE;
    

    But be careful because it will interrupt the response flow: it will be the last action executed.

    Have a good day!