douyigua5381 2018-07-13 21:01
浏览 20
已采纳

GoLang与sw-sagger响应头

I am pretty new to golang and trying to set my response headers. I have two headers that I want to set. I think that I am misunderstanding something fundamental. I am also using go-swagger to generate my endpoints.

My problem is that I can only seem to set one of my two headers. Swagger provides a function "auth.NewAuthLoginUserOK().WithProfileHeader("pickles)" on the return (in the "if success" block). How can I set two header params?

func AuthLoginRouteHandler(params auth.AuthLoginUserParams) middleware.Responder {
    transactionId := redFalconLogger.GetTransactionId()
    redFalconLogger.LogDebug("AuthLoginRouteHandler", transactionId)

    email := params.Body.Email
    password := params.Body.Password

    //Check to ensure that they are not nil
    if email == "" || password == ""{
        redFalconLogger.LogError("Got an empty string on a username/password", transactionId)
        return auth.NewAuthLoginUserBadRequest()
    }

    //use pointers to limit in flight private data
    pointerEmail := &email
    pointerPassword := &password

    //Call the auth domain
    success := authDomain.LoginUser(pointerEmail,pointerPassword,transactionId)

    if success {
        return auth.NewAuthLoginUserOK().WithProfileKeyHeader("pickles")
    }
    redFalconLogger.LogDebug("Failed Login: ", transactionId)
    return auth.NewAuthLoginUserBadRequest()
}

Thank you in advance.

  • 写回答

1条回答 默认 最新

  • dongrong9938 2018-07-14 01:04
    关注

    go-swagger will generate one method per response header defined in the spec on result objects (what's returned by auth.NewAuthLoginUserOK())

    If you have multiple response headers defined in the spec you generated off of, just chain the calls.

    return auth.NewAuthLoginUserOK().WithProfileKeyHeader("pickles").WithOtherHeader("cucumbers")
    

    You should try and avoid deviating from the specification as much as possible. If you absolutely need to write a header not specified in the spec, the response object will have a ServeHTTP method you can use to get at the stdlib's ResponseWriter.

        return auth.NewAuthLoginUserOK().ServeHTTP(func(rw http.ResponseWriter, r *http.Request) {
            // Try and avoid this
            rw.Header().Add("profile", "pickles")
            rw.Header().Add("other-header", "cucumbers")
        })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!