2017-08-30 16:47
浏览 112

Gorilla Mux中的config CORS:POST请求出现403错误

I have an API, currently am trying to consume one of its endpoints. The endpoint is for POST requests, the endpoint is working as expected. The API is running in the cloud, I tested it with curl and it was perfect, then from my react app I was trying to consume it but I get 403 status code.

Watching in the console of the browser I see that I get that error on a OPTIONS request, and the POST never get done. Here is a screenshot of the result displayed in the console:

enter image description here

Then, I made a simple HTML file with a form, there I placed the required inputs, and the action pointing to this endpoint and it worked pretty well. Then, I don't know where would be the error? I have enabled CORS in the API

In the API I am using Gorilla/mux and I have something like this:

// Set up a router and some routes
    r := mux.NewRouter()
    r.HandleFunc("/", handleHome)
    //some other routes

    headersOk := handlers.AllowedHeaders([]string{"*"})
    originsOk := handlers.AllowedOrigins([]string{"*"})
    methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})

    // Start http server
    port := fmt.Sprintf(":%d", SomePort)
    http.ListenAndServe(port, handlers.CORS(originsOk, headersOk, methodsOk)(r))



The message that I am getting in the browser is (in Spanish):

Solicitud desde otro origen bloqueada: la política de mismo origen impide leer el recurso remoto en https://miURL (razón: falta la cabecera CORS 'Access-Control-Allow-Origin').

In English: basically the server is rejecting the request because the CORS header is not present.

So, what have I done wrong in my router configuration?

图片转代码服务由CSDN问答提供 功能建议

我有一个API,当前正在尝试使用其端点之一。 该端点用于POST请求,该端点按预期方式工作。 该API在云中运行,我使用 curl 对其进行了测试,它是完美的,然后从我的react应用程序中尝试使用它,但得到了 403状态代码

在浏览器的控制台中观看,我发现我在OPTIONS请求中遇到了该错误,并且POST从未完成。 这是控制台中显示的结果的屏幕截图:


然后,我做了 一个带有表单的简单HTML文件,我在其中放置了所需的输入,并且操作指向了此端点,并且效果很好。 然后,我不知道错误在哪里? 我已经在API中启用了CORS

在API中,我正在使用Gorilla / mux,并且我有类似的东西:

  // 设置路由器和一些路由
r:= mux.NewRouter()
 r.HandleFunc(“ /”,handleHome)
 headersOk:= handlers.AllowedHeaders([] string {  “ *”})
 originsOk:= handlers.AllowedOrigins([] string {“ *”})
 methodOk:= handlers.AllowedMethods([] string {“ GET”,“ HEAD”,“ POST”,“ PUT  “,” OPTIONS“})
端口:= fmt.Sprintf(”:%d“,SomePort)
 http.ListenAndServe(端口,handlers.CORS(originsOk,headersOk,MethodsOk)  )(R))


 <代码> “” 


起源于官方的请求书:不合法的来源 隐含的证据 https:// miURL (razón:f alp la cabecera CORS'Access-Control-Allow-Origin')。



  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题