Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Functions REST API: Creating a new action from a zip file

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:[email protected]/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.

like image 944
user3024710 Avatar asked Dec 08 '25 06:12

user3024710


1 Answers

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:[email protected]/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\" ]"
like image 63
Vidyasagar Machupalli Avatar answered Dec 12 '25 04:12

Vidyasagar Machupalli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!