apache/openwhisk-runtime-go

Custom http status after function invocation

xemul opened this issue · 3 comments

xemul commented

Hello,

I'm trying to write a web action that can return back different http status codes, but docs say nothing about it. I looked at the code and found that the launcher.go just forwards the returned map back to executor and the executor itself doesn't set any status into ResponceWriter (thus defauling it to StatusOK?).

If someone would want to patch the launcher/executor to allow actions return custom status, what would be the best way to do it?

As with all webactions you can set the statusCode property on the response.

> cat hello.go
package main

func Main(args map[string]interface{}) map[string]interface{} {
	name, ok := args["name"].(string)
	if !ok {
		name = "stranger"
	}
	msg := make(map[string]interface{})
	msg["body"] = "Hello, " + name + "!"
	msg["statusCode"] = 201
	return msg
}

you can create the action as any other action and turn it into a web action for API calls;

> wsk action create hello hello.go --web true

then test the url with curl and see the response code is 201 in this case.

See docs here https://github.com/apache/openwhisk/blob/master/docs/webactions.md#handling-http-requests-with-actions.

In other words, @xemul do not need to patch the launcher or modify the runtime at all. The function itself can dictate the headers, status code and body of the response.

xemul commented

Thanks! I should have read the docs more carefully.