简体   繁体   中英

Pass current URL parameters to Cloud Function Python API on IBM Cloud

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.

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)

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM