0

I'm new to python and cannot figure out how to do the following. I want to put a variable inside a command but its not working. The command is taking the variable name not its value.

The script below calls https with username and password to get a token. I token is returned . I then need to use the token to create a user. I'm having issues with it, "token" in the iplanet pair is not getting expanded correctly. It is set correctly as I can print it out before the command. So token would contain something like "AQIC5wM2LY4Sfcydd5smOKSGJT" , but when the 2nd http call is made it gets passed the word token , rather than tokens value.

    import requests
    import json

    url = "https://www.redacted.com:443/json/authenticate"

    headers = {
        'X-Username': "user",
        'X-Password': "password",
        'Cache-Control': "no-cache",
        }

    response = requests.request("POST", url, headers=headers)

    tokencreate = json.loads(response.text)
    token=tokencreate['tokenId']
    print token

    url = "https://www.redacted.com:443/json/users"

    querystring = {"_action":"create"}

    payload = "{\r\n\"username\":\"Patrick\",\r\n\"userpassword\":\"{{userpassword}}\",\r\n\"mail\":\"[email protected]\"\r\n}"
    headers = {
        'iPlanetDirectoryPro': "token",
        'Content-Type': "application/json",
        'Cache-Control': "no-cache",
        }

    response = requests.request("POST", url, data=payload, headers=headers, params=querystring)

    print(response.text)
4
  • What do you mean "is not getting expanded correctly"? Commented Jan 26, 2018 at 19:56
  • I've update the question . token contains something like "AQIC5wM2LY4Sfcydd5smOKSGJTgfvMJSNtRL0NwU_iKoRg8.*AAJTSQACMDIAAlNLABM3MTU4NzU5OTI5Mzk3MTA3ODEyAAJTMQACMDE.*" but the command is just being run with the word token not the string. Commented Jan 26, 2018 at 20:02
  • Try to change the line 'iPlanetDirectoryPro': "token", to 'iPlanetDirectoryPro': token, (remove quotation marks around token) Commented Jan 26, 2018 at 20:06
  • Cheers , thats fixed it :) Commented Jan 26, 2018 at 20:08

1 Answer 1

2

It's because you are passing the string 'token' when you mean to pass the variable token

here you create the token:

tokencreate = json.loads(response.text)
token=tokencreate['tokenId']
print token

But you don't use the actual variable, it should look like this:

payload = "{\r\n\"username\":\"Patrick\",\r\n\"userpassword\":\"{{userpassword}}\",\r\n\"mail\":\"[email protected]\"\r\n}"
headers = {
    'iPlanetDirectoryPro': token,
    'Content-Type': "application/json",
    'Cache-Control': "no-cache",
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Although if I wanted " " around the value would I have to use \" token \" ?
Not sure why you would do that, to cast it as a string? You can use string formating if the quotes are important, like this: "'\"{}\"".format(token) That will give you a string that is equal to "{the value of your token here}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.