0

I need to be able to perform certain actions based on the http response code I get back from an http outbound endpoint. For instance if I get a 500 error or a 302 redirect or a 200. I need a way to evaluate the 500, 302, 200. I know how to use Choice-When, but don't know how to access the response code using groovy or whatever you suggest.

Brett AME
  • 5
  • 1
  • 2
  • 6

2 Answers2

11

Seba is right but that is not enough.

By default, if an client or server error is detected in an HTTP outbound interaction (ie response code >= 400), Mule will treat the response as an error and will break the flow execution and call the exception strategy to deal with the error.

You need to deactivate this behaviour before doing the HTTP outbound interaction in order to have the rest of the flow (your choice router) be called. So you need this:

<set-variable variableName="http.disable.status.code.exception.check"
              value="true" />

before your HTTP outbound endpoint.

David Dossot
  • 33,403
  • 4
  • 38
  • 72
  • will this mess with Mule's process in terminating socket connections? – EdC Nov 27 '14 at 22:22
  • The usage of the misnamed `returnException` method (should be `throwException`) in https://github.com/mulesoft/mule/blob/mule-3.x/transports/http/src/main/java/org/mule/transport/http/HttpClientMessageDispatcher.java is hard to follow but it seems that for `one-way` outbound (ie `doDispatch`) the connection management behaviour won't change. For `request-response` (ie `doSend`) it seems that Mule will keep outbound connections open even in case of bad status code. It shouldn't be detrimental though since the connection is dealt in the dispatcher, which is a managed pooled object. – David Dossot Nov 28 '14 at 17:22
  • I think it's worth opening a JIRA so MuleSoft can investigate the problem. – David Dossot Nov 28 '14 at 19:16
  • This is different now, you can use Status Code Settings and say that 200,404 are allowed or 200,500, or a range with "..", whatever you need so that you can allow "bad" statuses through, either just a few, or all and then trap them with a choice after. – Karl Henselin Apr 15 '20 at 16:44
9

You can get the HTTP response code with the following expression right after the HTTP outbound endpoint:

#[message.inboundProperties['http.status']]

Likewise in a Groovy script:

message.getInboundProperty('http.status')
Seba
  • 2,319
  • 15
  • 14