https://linen.dev logo
Title
l

Leon Witznick Schumacher

03/21/2023, 9:14 PM
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

Rob Elliot

03/22/2023, 11:06 AM
You need to stub the
OPTIONS
request that the browser will make as a preflight request to have the CORS headers:
{
  "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

Tom

03/22/2023, 11:54 AM
You’re probably better off enabling stub CORS rather than trying to set the correct headers yourself. This is either via
.stubCorsEnabled(true)
in Java or
--enable-stub-cors
on the CLI
r

Rob Elliot

03/22/2023, 11:55 AM
Ah, thanks Tom, didn't know WireMock could do it for you
t

Tom

03/22/2023, 11:55 AM
A relatively recent addition
l

Leon Witznick Schumacher

03/22/2023, 6:07 PM
Thanks