Skip to content

Commit

Permalink
adapt example to also work with post
Browse files Browse the repository at this point in the history
Signed-off-by: Luke Roy <luke.roy@ibm.com>
  • Loading branch information
Luke-Roy-IBM committed Jul 29, 2024
1 parent 6e97a9d commit 6c2e6ff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
21 changes: 19 additions & 2 deletions helloworld-samples/function-http-nodejs/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
module.exports.main = main = async (args) => {
const url = 'https://httpbin.org/get';
const response = await fetch(url);
let url = 'https://httpbin.org/';
requestData = {}
if (args["__ce_method"] == "POST") {
url = url + "post"
requestData = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(args)
}
} else {
url = url + "get"
requestData = {
method: 'GET'
}
}

const response = await fetch(url, requestData);
const data = await response.json();

return {
Expand Down
13 changes: 10 additions & 3 deletions helloworld-samples/function-http-python/__main__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import http.client
import http.client, urllib.parse
import json


def main(params):

url = "httpbin.org"
endpoint = "/get"
connection = http.client.HTTPSConnection(url)
connection.request("GET", endpoint)

if params["__ce_method"] == "POST":
endpoint = "/post"
connection.request("POST", endpoint, json.dumps(params), {"Content-Type": "application/json"})
else:
endpoint = "/get"
connection.request("GET", endpoint)


response = connection.getresponse()
data = response.read()
connection.close()
Expand Down

0 comments on commit 6c2e6ff

Please sign in to comment.