OlexandrStepanov / swift-webaction-binary

Sample for OpenWhisk web action using Swift binary.
Apache License 2.0
0 stars 1 forks source link

When using web action with API Gateway, 'body', 'headers' and 'statusCode' are not interpreted #2

Closed OlexandrStepanov closed 6 years ago

OlexandrStepanov commented 6 years ago

If OpenWhisk action from this sample deployed as Web action, then it got assigned URL of look: https://openwhisk.ng.bluemix.net/api/v1/web/<iser_id>/default/swift-packages-dev-time If this URL used with .json ending, it's output is not interpreted, but returned as is, e.g. has next look:

{ "body": "Base_64_string", "headers": { ... }, "statusCode": <int> }

This is an expected behaviour for Web actions, according to the documentation.

However, if this action is then used in API Gateway, result is not interpreted as well, e.g. has the same look as when web action URL used with .json ending. And there is no option to tweak this. This makes using web actions in API Gateway useless.

jthomas commented 6 years ago

The API Gateway services supports a "response type" configuration parameter to control how it interprets the response from the web action before returning it to the client.

This defaults to json, which passes back the JSON response from the web action without intepreting the content. If you update the API gateway endpoint to use the http content type, this will use the response body to control the HTTP response. https://github.com/apache/incubator-openwhisk/blob/master/docs/apigateway.md#full-control-over-the-http-response

$ bx wsk api create /api/time get swift-packages-dev-time --response-type http
ok: created API /api/time GET for action /_/swift-packages-dev-time
https://service.us.apiconnect.ibmcloud.com/gws/apigateway/api/1310a834667721bb9bf6968e828aa286aa5a287b4e5d46a513aa813a775602fb/api/time
$ http get https://service.us.apiconnect.ibmcloud.com/gws/apigateway/api/1310a834667721bb9bf6968e828aa286aa5a287b4e5d46a513aa813a775602fb/api/time x-client-id:SOME_CLIENT_ID
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS
Access-Control-Allow-Origin: *
Content-Length: 25
Content-Type: application/json
Date: Mon, 29 Jan 2018 17:22:42 GMT
Ibm_cloud_functions: OpenWhisk
Server: openresty
Set-Cookie: DPJSESSIONID=PBC5YS:1663067204; Path=/; Domain=.whisk.ng.bluemix.net
X-Backside-Transport: OK OK
X-Gateway-Host: 10.121.224.202:31433
X-Global-Transaction-Id: 2887033057
X-Request-Id: 3QkWp4QTUKhDA7mlBgKyOc8ykeMplXRV

{
    "unixtime": "1517246562"
}

This configuration parameter can be set in the serverless.yml as below.

functions:
  my_function:
    handler: index.main
    events:
      - http:
          method: GET
          path: /api/http
          resp: http

Does that resolve the issue?

OlexandrStepanov commented 6 years ago

I was creating API Gateway through web interface, and it doesn't have such option there. Thanks, @jthomas, issue is solved now.