0

I'm new and trying to create a Cloud Function on IBM Cloud. My API is working fine on "hello world" only. I need to pass the parameters from URL to manipulate it into my Python API. Like:

URL: https://fa75e0fa.eu-gb.apigw.appdomain.cloud/testapi/test1?id=11

I need to pass the value of id=11 at the end of the above URL into my python code (Python 3.70).

I have now this:

#
#
# main() will be run when you invoke this action
#
# @param Cloud Functions actions accept a single parameter, which must be a JSON object.
#
# @return The output of this action, which must be a JSON object.
#
#
import sys

def main(dict):
    return { 'message': 'Hello world' }

The output is: { "message": "Hello world" }

I tried:

import sys
import urllib3, requests, json
import requests
import os

def main(dict):
    id1=requests.GET.get('id')

    return { 'message': 'Hello world',
             'id': json.loads(id1.text)

    }

The output is:

Activation ID: 4e97b3be9b2b49f397b3be9b2b99f34d Results: { "error": "module 'requests' has no attribute 'GET'" } Logs:

[ "2020-04-16T11:29:34.215661Z stderr: Traceback (most recent call last):", "2020-04-16T11:29:34.215717Z stderr: File \"/action/1/src/exec__.py\", line 66, in ",
"2020-04-16T11:29:34.215722Z stderr: res = main(payload)",
"2020-04-16T11:29:34.215725Z stderr: File \"/action/1/src/main__.py\", line 10, in main",
"2020-04-16T11:29:34.215728Z stderr: id1=requests.GET.get('id')",
"2020-04-16T11:29:34.215731Z stderr: AttributeError: module 'requests' has no attribute 'GET'", "2020-04-16T11:29:34.215734Z
stderr: " ]

Can you please help? Thanks.

data_henrik
  • 16,724
  • 2
  • 28
  • 49
mg g
  • 21
  • 4

1 Answers1

0
def main(args):
    arg1=args.get("arg1")
    myarg=args.get("anotherarg")

    return {"one": arg1, "two": myarg}

The parameters are passed in the environment which you can easily access. They are not part of a request structure. arg1 would be your id:

import sys
import urllib3, requests, json
import requests
import os

def main(dict):
    id1=dict.get('id')

    return { 'message': 'Hello world',
             'id': id1)

    }
data_henrik
  • 16,724
  • 2
  • 28
  • 49
  • But does it mean that I will have 2 main functions in the script, please? – mg g Apr 16 '20 at 11:48
  • I mean I will have main(dict) and main(args)? please? – mg g Apr 16 '20 at 11:53
  • I tried only main(args) with the above code. However, it gives me:{ "code": "5559f9968f11637895386cd3d5ec57d9", "error": "The requested resource does not exist." } when launching the URL:https://fa75e0fa.eu-gb.apigw.appdomain.cloud/testapi/test1?id=11 – mg g Apr 16 '20 at 11:56
  • I named it differently. You can name it dict or args or params. Use get() to access the variable you are looking for and which was passed in as parameter. – data_henrik Apr 16 '20 at 12:02
  • And here is a full example which fetches from Wikipedia: https://github.com/data-henrik/chatbot-talks/blob/2020letsmake/functions/wikiExtract.py – data_henrik Apr 16 '20 at 12:03
  • Thanks a lot... Worked like a charm! :) – mg g Apr 16 '20 at 16:29
  • Another question please, I know you are expert in Watson Assistant Chatbot too :). I want to save a variable from the user input by enabling Slots and system entities like @sys-number.. I can save number by choosing check for sys-number, or saving date by choosing check for sys-date... But for text/characters please I can't find sys-text or sys-char.. So, how can I use Slots to save text, please? Thanks – mg g Apr 16 '20 at 16:33
  • The process is that you mark answered questions as such and upvote them. And you open new questions for new questions. This is not support, but knowledge-sharing... :) – data_henrik Apr 17 '20 at 05:59
  • Thans a lot again... I will open a new question for that.. Hope you will be there to answer :) Upvote Done now :) I will mark question as answered.. Thanks a lot again! – mg g Apr 17 '20 at 10:47