Home > Enterprise >  Mule design centre defining 400 response but trying it does not return same response
Mule design centre defining 400 response but trying it does not return same response

Time:11-19

I am defining my api contract in RAML in Mule Anypoint platform Design centre . Here is the simple contract :

    #%RAML 1.0
title: test_experiment
version: v1
mediaType: application/json

/test:
  put:
    headers:
      trackingId:
        type: string
        description: "Track each request"
        minLength: 3
    responses:
      200:
        body:
          application/json:
            example:
              {
                "msg": "successfully done"
              }

      400:
        body:
          application/json:
            example:
              {
                "msg": "something bad was submitted",
                "id" : "001"
              }

Next I am trying to 'test it' in Documentation tab and when I purposefully do not enter a trackingId I do get a 400 response code but the response payload is different.

I have defined the response payload as : ( expected payload )

 {
     "msg": "something bad was submitted",
     "id" : "001"
 }

However the payload response in 'try it' in design centre is :

{
  "code": "REQUEST_VALIDATION_ERROR",
  "message": "Error validating header: expected minLength: 3, actual: 0"
}

So why is the 400 response not being returned as I have it defined in my raml ? enter image description here

Edit1 : This behaviour is all observed in Design Centre , I am not providing an implementation and then testing it , I am using design centre Documentation and 'Try it' feature as per image below : enter image description here

CodePudding user response:

RAML only defines specifications that you are planning to send as a response. It really cannot do anything if your actual implementation breaks those specifications. It is not the job of the RAML or APIKIT router to monitor the actual responses.

You should be having an "apikit router" component your application. The initial request validation takes place within this component. If it finds the request to be invalid, then it throws APIKIT:BAD_REQUEST. You need to look at your error handler which is handling this error. You may have defined either at flow level or as a Global Error Handler and update the dataweave within those error handlers to send the response in the structure that you have specified in your RAML.

CodePudding user response:

Because it is a mock, not a real implementation. When you are testing in Design Center you are using the Mocking Service that simulates reponses. The error you are getting is because the request doesn't match with the specification. The Mocking Service usually uses only the first status code it finds in the API RAML. If you want it to return your error use a behavioral header like MS2-Status-Code to set the error code. For example set the header MS2-Status-Code to 200,400. Although I'm not sure it will work to override the validation error from the Mocking Service. You will need to try.

  • Related