SUP, I'm with a problem using the method post: Er...
# general
l
SUP, I'm with a problem using the method post: Error: Access to XMLHttpRequest at 'http://localhost:8080/api/v1/project' from origin 'http://localhost:5173' has been blocked by CORS policy { "request": { "method": _"_POST_",_ "urlPath": _"_/api/v1/project_",_ "headers": { "Content-Type": { "equalTo": _"_application/json_"_ } } }, "response": { "status": 200, "bodyFileName": _"_files/project_POST.json_",_ "headers": { "Content-Type": _"_application/json_",_ "Access-Control-Allow-Origin" : _"_*_",_ "Access-Control-Allow-Methods": _"_GET, POST, PUT, DELETE, OPTIONS_"_ }, "fixedDelayMilliseconds": 500 } }
👀 1
r
You need to stub the
OPTIONS
request that the browser will make as a preflight request to have the CORS headers:
Copy code
{
  "request": {
    "method": "OPTIONS",
    "urlPath": "/api/v1/project"
  },
  "response": {
    "status": 204,
    "headers": {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
      "Access-Control-Allow-Headers": "*"
    }
  }
}
t
You’re probably better off enabling stub CORS rather than trying to set the correct headers yourself. This is either via
Copy code
.stubCorsEnabled(true)
in Java or
--enable-stub-cors
on the CLI
r
Ah, thanks Tom, didn't know WireMock could do it for you
t
A relatively recent addition
l
Thanks