4

I'm using beego framework as my API framework and AngularJS on the client. I have set all CORS setting correctly. I can do GET request. But, when i try to POST, beego treat is as OPTIONS request. It also throw a warning: multiple response.WriteHeader calls. what could possibly wrong?

my beego CORS setting:

func init() {
    orm.RegisterDataBase("default", "mysql", "root:@tcp(127.0.0.1:3306)/fakeapi")
    beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
        AllowOrigins:     []string{"*"},
        AllowMethods:     []string{"GET", "DELETE", "PUT", "PATCH", "POST"},
        AllowHeaders:     []string{"Origin"},
        ExposeHeaders:    []string{"Content-Length"},
        AllowCredentials: true,
    }))

}

My ANgularJS request

var transaction = $http.post(BASE_URL + "transaction", transactionData);
                return $q.all([transaction]).then(function(response) {
            console.log(response);
        });

my system: Ubuntu 14.04 beego: 1.4.2 bee: 1.2.4 angularJS: 1.3.12

under5hell
  • 997
  • 3
  • 16
  • 40

2 Answers2

2

That might because of an issue/pull request currently pending to be merged into master: issue 912

Without this line everything is fine:: router.go#L861

That seems to be in line with commit 3bb4d6f which shows:

// Write status code if it has been set manually
// Set it to 0 afterwards to prevent "multiple response.WriteHeader calls"

(and router.go do set a status, hence the error message)

Commit f962457 is supposed to solve this issue, but isn't merged yet.


The other issue 904 mentions something about being unable to retrieve the Session data previously registered in the Session Engine. Maybe Session.on flag can help.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • thanks for your response. i have delete that line mention in commit f962475. but the error persists. – under5hell Feb 05 '15 at 07:12
  • @under5hell ok, I have only found one other instance of this error involving Session (I have edited the answer). But it is more a clue than a definitive answer. – VonC Feb 05 '15 at 07:42
0

I handle it like that, I hope it helps

import (
_ "info_apoyo/routers"

"github.com/astaxie/beego"
"github.com/astaxie/beego/plugins/cors"
)

func main() {
if beego.BConfig.RunMode == "dev" {
    beego.BConfig.WebConfig.DirectoryIndex = true
    beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
}
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
    AllowOrigins:     []string{"*"},
    AllowMethods:     []string{"GET", "POST", "DELETE", "PUT", "PATCH"},
    AllowHeaders:     []string{"Origin", "content-type", "Access-Control-
Allow-Origin"},
    ExposeHeaders:    []string{"Content-Length", "Access-Control-Allow-
Origin"},
    AllowCredentials: true,
}))
beego.Run()
}
  • THIS IS SHOWING ERROR with beego api cannot use cors.Allow(&(cors.Options literal)) (value of type beego.FilterFunc) as web.FilterFunc value in argument to beego.InsertFilter – Piyush Raj May 10 '21 at 19:05