You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2022/02/18 01:00:27 UTC
[GitHub] [openwhisk-runtime-go] DonRichie opened a new issue #161: Failing to write a response containing headers
DonRichie opened a new issue #161:
URL: https://github.com/apache/openwhisk-runtime-go/issues/161
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.
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.
Can someone provide an example how to set headers in the golang response?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@openwhisk.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [openwhisk-runtime-go] DonRichie edited a comment on issue #161: Failing to write a response containing headers
Posted by GitBox <gi...@apache.org>.
DonRichie edited a comment on issue #161:
URL: https://github.com/apache/openwhisk-runtime-go/issues/161#issuecomment-1043713063
**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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@openwhisk.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [openwhisk-runtime-go] DonRichie commented on issue #161: Failing to write a response containing headers
Posted by GitBox <gi...@apache.org>.
DonRichie commented on issue #161:
URL: https://github.com/apache/openwhisk-runtime-go/issues/161#issuecomment-1043713063
**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)
// set headers
headers := make(map[string]interface{})
headers["kek"] = "kok"
headers_bin_str,err := json.Marshal(headers)
headers_str := string(headers_bin_str)
if err != nil {
fmt.Println(err)
}
// assemble response
outobj := make(map[string]interface{})
fmt.Println(headers_str)
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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@openwhisk.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [openwhisk-runtime-go] DonRichie commented on issue #161: Failing to write a response containing headers
Posted by GitBox <gi...@apache.org>.
DonRichie commented on issue #161:
URL: https://github.com/apache/openwhisk-runtime-go/issues/161#issuecomment-1043697443
**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
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@openwhisk.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org