Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing to write a response containing headers #161

Open
DonRichie opened this issue Feb 18, 2022 · 3 comments
Open

Failing to write a response containing headers #161

DonRichie opened this issue Feb 18, 2022 · 3 comments

Comments

@DonRichie
Copy link

DonRichie commented Feb 18, 2022

Hello,

I am currently trying to create a response containing headers in openwhisk with golang.
I feel like I need a nested map with different value types. But since golang is strongly-typed that is impossible.
I tried to marshal a struct, but failed since the Main functions wants a map as return value.

My best try as response is:

  outobj := make(map[string]interface{})
  outobj["headers"] = "{\"Content-Type\": \"text/html\"}"
  outobj["statusCode"] = 201
  outobj["body"] = "123"
  return outobj

But invoking that action gives an http error.
The reason for that error is the content of the "headers" key. Uncommenting it makes the action work.

Can someone provide an example how to set headers in the golang response?

@DonRichie
Copy link
Author

Here is my best try using a struct, but it fails due to the wrong function signature:

package main

import (
  "fmt"
  "encoding/json"
)

type WhiskResponse struct {
  body string
  statusCode int
  headers map[string]interface{}
}

func PrettyPrint(v interface{}) (err error) {
      b, err := json.MarshalIndent(v, "", "  ")
      if err == nil {
              fmt.Println(string(b))
      }
      return
}

func Main(inobj map[string]interface{}) map[string]interface{} {
  fmt.Println("inobj:")
  PrettyPrint(inobj)
  
  // assemble response
  resp := WhiskResponse{}
  resp.headers["Content-Type"] = "text/html"
  resp.statusCode = 201
  resp.body = "123"
  fmt.Println("resp:")
  PrettyPrint(resp)
  res, err := json.Marshal(resp)
  if err != nil {
    fmt.Println(err)
  }
  return string(res)
}

error:

./exec__.go:37:16: cannot use string(res) (type string) as type map[string]interface {} in return argument

error if changing function signature to "func Main(inobj map[string]interface{}) string {":

./main__.go:56:9: cannot use Main (type func(map[string]interface {}) string) as type Action in assignment

@DonRichie
Copy link
Author

DonRichie commented Feb 18, 2022

I actually managed to set a header:

package main

import (
  "fmt"
  "encoding/json"
)

func PrettyPrint(v interface{}) (err error) {
      b, err := json.MarshalIndent(v, "", "  ")
      if err == nil {
              fmt.Println(string(b))
      }
      return
}

func Main(inobj map[string]interface{}) map[string]interface{} {
  fmt.Println("inobj:")
  PrettyPrint(inobj)
  
  // assemble response
  outobj := make(map[string]interface{})
  header_map := make(map[string]string)
  header_map["headername"]= "headerval"
  outobj["headers"] = header_map
  outobj["statusCode"] = 201
  outobj["body"] = "123"
  fmt.Println("outobj:")
  PrettyPrint(outobj)
  return outobj
}

I hereby request to extend the documentation with a nice example.

@sciabarracom
Copy link
Contributor

please send a pr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants