Search code examples
pythonpython-requestsgoogle-colaboratory

Pass string parameter to user input in an api request


I am using python to make requests to one of the google apis.

I was using this code which works.

from oauth2client.service_account import ServiceAccountCredentials
import httplib2

SCOPES = [ "https://www.googleapis.com/auth/url-api" ]
ENDPOINT = "url-endpoint"

JSON_KEY_FILE = "route json"

credentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEY_FILE, scopes=SCOPES)

http = credentials.authorize(httplib2.Http())

content = """{
  "url": "domain.com",
  "type": "TYPE-NOTIFICATION"
}"""

response, content = http.request(ENDPOINT, method="POST", body=content)
print(response)

I am trying to make the parameters that build the variable "content" work as a user input.

As you can see, right now the "content" variable consists of a string literal. This is causing me problems to transform each of these parameters into an input.

How can I solve this? My goal is that a user can enter a value for url. The same with the "type" parameter.

I have tried removing the string and adding an input for the values, but this does not work.


Solution

  • Construct the user inputs into a dictionary. Like below.

    content = {}

    content["url"] = input("Enter URL")

    content["type"] = input("Enter Type")

    I guess, the body parameter can be passed as dictionary. So you can pass the above "content" variable directly to the call.

    If only string works, convert the variable to a string.

    content = str(content)