1

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.

mornindew
  • 1,993
  • 6
  • 32
  • 54

2 Answers2

0

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")
    })
tonymke
  • 752
  • 1
  • 5
  • 16
  • Thank you. You are exactly right!!! I forgot that i could "chain" them in golang. Coming from a Java world I expected the method on the right to act on the response from the first. This was exactly the answer that I needed. Also - I agree on not deviating from the spec but nice to see how it could be done. – mornindew Jul 14 '18 at 01:07
0

You can try below solution. Define like this in your swagger.yml

/deviceProvisioningDetails/{deviceId}:
    get:
      tags:
        - tenantManager
      operationId: getDeviceID
      parameters:
            - name: deviceId
              in: path
              description: Device ID
              required: true
              type: string
            - name: requestId
              in: header
              required: true
              description: "request id"
              type: string
      responses:
        200:
          description: OK
          headers:
            tenantId:
              type: string
              description: "Tenant Id"

Then in your configure.go you can return with payload.

  return tenant_manager.NewGetDeviceIDOK().WithTenantID(tenantId)
infiniteLearner
  • 3,555
  • 2
  • 23
  • 32