-2

I am using servant-options packages as described in the documentation:

type RepositoryAPI = "repository" :> Get '[JSON] Repository 

getRepository :: Handler Repository
getRepository = do
  repository <- liftIO loadRawRepositoryFromFilesystem 
  return repository

server :: Server RepositoryAPI 
server = getRepository 

repositoryAPI :: Proxy RepositoryAPI 
repositoryAPI = Proxy

app :: Application
app = logStdoutDev
  $ cors (const $ Just policy)
  $ provideOptions repositoryAPI 
  $ serve repositoryAPI server
    where policy = simpleCorsResourcePolicy { 
          corsMethods = ["OPTIONS", "GET", "PUT", "POST"],
          corsRequestHeaders = [ "Authorization", "Content-Type" ] 
    }

main :: IO ()
main = run 8081 app

But OPTIONS request to the URL still returns error 400:

OPTIONS /repository
  Accept: */*
  Status: 400 Bad Request 0.000039136s

What am I doing wrong? Tried with different policy configurations (no "Authorization" or no corsMethods or with lower case "content-type"), but still no luck. Could this be something I need to handle on the front-end? Any advice is appreciated.

altern
  • 5,829
  • 5
  • 44
  • 72

1 Answers1

-1

The issue was on front-end. I had to do the following call with d3.json method:

json("http://localhost:8081/repository", 
    {method: 'GET', headers: new Headers(
         [['Access-Control-Request-Headers', "content-type"]]
    )}
)

Previously I was doing the following:

json("http://localhost:8081/repository", 
    {method: 'GET', headers: new Headers(
         [['Access-Control-Allow-Origin', "*"]]
    )}
)

So now it all works.

altern
  • 5,829
  • 5
  • 44
  • 72