3

I'm trying to create a nodejs function from a zip file and through the REST API using the following curl:

curl --request PUT --url 'https://my:credentials@openwhisk.eu-gb.bluemix.net/api/v1/namespaces/mynamespace/actions/my_action_name?overwrite=true' --header 'accept: application/json' --header 'content-type: application/json' --data '{"annotations":[{"key":"exec","value":"nodejs:10"},{"key":"web-export","value":true}],"exec":{"kind":"nodejs:10","init":"./action.zip"},"parameters":[{"key":"message","value":"Hello World"}]}'

As a result I get an error:

"error":"The request content was malformed:\n'code' must be a string or attachment object defined in 'exec' for 'nodejs:10' actions"

Is it possible to get an example of how to create a new action from a zip file and through the REST API? Thank you.

user3024710
  • 515
  • 1
  • 6
  • 15

2 Answers2

3

You have to base64 encode your .zip file and then pass it as a code parameter. I have written a shell script(bash) to encode and also create an action called 'action'. Save the script as create.sh and execute the script ./create.sh

#!/bin/sh
ACTION=action
ZIP=$ACTION.zip

base64 $ZIP | echo "\"$(cat)\"" | jq "{namespace:\"_\", name:\"$ACTION\", exec:{kind:\"nodejs:10\", code:., binary:true, main:\"main\"}}" | curl -X PUT -H "Content-Type:application/json"  -d @- https://USERNAME:PASSWORD@openwhisk.ng.bluemix.net/api/v1/namespaces/_/actions/$ACTION?overwrite=true

Complete code

app.js or index.js code

function myAction(args) {
    const leftPad = require("left-pad")
    const lines = args.lines || [];
    return { padded: lines.map(l => leftPad(l, 30, ".")) }
}

exports.main = myAction;

package.json

{
  "name": "node",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "left-pad" : "1.1.3"
  }
}

Run npm install and zip the file zip -r action.zip *.

To test the action

ibmcloud fn action invoke --result action --param lines "[\"and now\", \"for something completely\", \"different\" ]"
Vidyasagar Machupalli
  • 2,737
  • 1
  • 19
  • 29
1

The REST API for creating or updating a Cloud Functions actions is documented in the IBM Cloud Functions API docs. A good way to find out the exact curl / request syntax is to use the IBM Cloud Functions CLI in verbose mode (-v). The CLI is just a wrapper around the REST API and in verbose mode all the REST details are printed.

Here is the relevant part for what could be printed:

Req Body
Body exceeds 1000 bytes and will be truncated
{"namespace":"_","name":"mytest/myaction","exec":{"kind":"nodejs:8","code":"UEsDBBQAAAAIAHJPhEzjlkxc8wYAAH8VAAALABwAX19tYWluX18ucHlVVAkAA+iFxFrohcRadXgLAAEE9AEAAAT0AQAAxVhtb9s2EP6uX8HRCCLBipb002DA69YkbYo17dZ0GwbDMGSKlrXJokfSToNh/313R+rNL2labJiK1iJ578/x7tTBgJ7A/QzYq8IuN3NmdbpYFIIZm9rC2EKYmiIYsB+1ynW6Ykqz1y9u2WWpNhl7uamELVTFrGJClaUUtha2LeQ9S6uMiVJVspYNgnDPWKVhb5lalqU2ZUXFUqZlmaKwtKTNeWpkzKp0JcsHdj

You would need to set the binary field to true and include the zip content as code. The curl docs suggest to use @filename to reference your zip file:

If you want the contents to be read from a file, use <@filename> as contents.

data_henrik
  • 16,724
  • 2
  • 28
  • 49