Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding headers to Jetty in Wiremock

Tags:

jetty

wiremock

I'm running into CORS issues using the Wiremock standalone jar. I call my mock service using jQuery ajax. Is it possible to add the required "Access-Control-Allow-Origin" header when starting up the server?

like image 230
dex Avatar asked Dec 19 '25 20:12

dex


1 Answers

I got it to work by adding an options.json file in my mappings folder for the CORS preflight request

{
  "request" : {
    "url" : "/myurl",
    "method" : "OPTIONS"    
  },
  "response" : {
    "status" : 200,
    "headers" : {
      "Access-Control-Allow-Origin" : "http://myorigin",
      "Access-Control-Allow-Headers": "accept, content-type",
      "Access-Control-Allow-Methods": "GET, POST"
    }
  }
}

and all my other mappings look like this

{
  "request" : {
    "urlPattern" : "/myurl",
    "method" : "POST",
    "bodyPatterns" : [ {
      "equalToJson" : "{\"foo\":0}",
      "jsonCompareMode" : "LENIENT"
    } ]
  },
  "response" : {
    "status" : 200,
    "bodyFileName" : "body-file.json",
    "headers" : {
      "Access-Control-Allow-Origin" : "*"
    }
  }
}

hope it helps


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!