I want to Store a Value Of ActivationCode That I see it in response.
This is my response in Pycharm terminal. Notice that I convert it to JSON with
${Data}= Evaluate json.loads("""${Response.content}""") json
Now This is my response after convert it to JSON
{'status': 'created', 'statusCode': '0001', 'message': {'type': 'success', 'text': 'ثبتâ\x80\x8cÙ\x86اÙ\x85 Ù\x85Ù\x88Ù\x81Ù\x82Û\x8cت Ø¢Ù\x85Û\x8cز بÙ\x88د'}, 'error': [], '
data': {'user': {'uuid': 'c6b65427-bbfa-4d25-ac3e-aa190d45f5d9', 'username': 'dropp45', 'phoneNumber': '09203947700', 'activationCode': '88261'}}}
How do I store activationCode in a variable for next step what is keyword to show 88261
Once the value is a normal dictionary, you access it with the keys' names:
${value}= Set Variable ${Data['data']['user']['activationCode']}
Log ${value} # will print 88261
You can just go to the Json Response like this:
${activationCode}= Set Variable ${Data}[data][user][activationCode] #You access activationCode
Log ${activationCode} # This will Log 88261
Related
I have the following response from a POST request:
{"facilities":[{"id":"f966a7d9-6a2d-43df-8cbf-ebdcb8c7fdc4","description":"luovbfvwofgdrcwvqtyqohjioocszgplcjh","hasAnyPartnership":false,"hasAnyProcedure":false}
So I used the "Convert String to JSON" function and got the following response:
{'facilities': [{'id': 'f966a7d9-6a2d-43df-8cbf-ebdcb8c7fdc4',
'description': 'luovbfvwofgdrcwvqtyqohjioocszgplcjh',
'hasAnyPartnership': False, 'hasAnyProcedure': False}
How do I get the ID value that is inside FACILITIES?
'facilities': [{'id': 'f966a7d9-6a2d-43df-8cbf-ebdcb8c7fdc4'
The JSON example you have provided is not the valid one. It is missing ] of facilities array and } of opening external brace. After correction it should look like this -
{"facilities":[{"id":"f966a7d9-6a2d-43df-8cbf-ebdcb8c7fdc4","description":"luovbfvwofgdrcwvqtyqohjioocszgplcjh","hasAnyPartnership":false,"hasAnyProcedure":false}]}
You can use following keywords from JSONLibrary
${json}= Convert String to JSON ${JsonVar}
${idValue}= Get Value From Json ${json} $.facilities[0].id
Output -
I am trying to pass the below API request in ROBOT framework:
curl --request POST --url <API End Point> --form 'mldata={"id": “idname”, "author": “author name, "title": “title name, "description": “description details“, "version": "0.0.4", "image": “/path/to/image”, "icon": "base64 encoded image"};type=application/json' --header "Authorization: <Bearer Token>”
Written the same in ROBOT framework [Version: 3.2.2], but getting error: "status=400, reason=Bad Request body={"message":"Request is not a multipart request!"}"strong text
It seems I am not passing the --form data in a right way. Request for help in passing the form data [--form 'mldata={"id": “idname”, "author": “author name, "title": “title name, "description": “description details“, "version": "0.0.4", "image": “/path/to/image”, "icon": "base64 encoded image"};type=application/json'] in robot framework.
How should I pass in the right way?
Onboard ML From External Repo
[Arguments] ${token}=default
Create Session session ${host}
${data}= Create Dictionary id=${id} author=${author} title=${title} description=${description} version=${version} image=${repo_path}
${form_data}= Evaluate {'mldata=': ${data}}
${header}= Create Dictionary Authorization=${token}
${response}= Post Request session /v1/ml data=${form_data} headers=${header}
Should Be Equal As Strings ${response.status_code} 200
I tried the below code and it worked. There are two modifications I had done from the original post.
No dictionary is created, instead all the items are directly evaluated
Instead of 'data', passing 'files' in post request, as for multi-form request, files is required.
Onboard ML From External Repo
[Arguments] ${token}=default
Create Session mxesession ${host}
# ${data}= Create Dictionary id=${id} author=${author} title=${title} description=${description} version=${version} image=${repo_path} "icon": ${icon_image}
${form_data}= Evaluate {'mldata': (None,'{"id": "${id}", "author": "${author}", "title": "${title}", "description": "${description}", "version": "${version}", "image": "${repo_path}", "icon": "${icon_image}"}')}
# ${form_data}= Evaluate {'mldata': "'"+${data}+"'"}
# ${form_data}= Evaluate {'mldata': str(${data})}
# ${form_data}= Evaluate {'mldata': (None,str(${data}))}
# ${form_data}= Evaluate {'mldata': (None,${data})}
${header}= Create Dictionary Authorization=${token}
${response}= Post Request session /v1/ml files=${form_data} headers=${header}
Should Be Equal As Strings ${response.status_code} 200
I am not sure is it the right way for passing multi-form data, but it worked in my case. If anyone has a better workable idea, please comment. It will be helpful for me to refine this code to a much better shape.
Updated the answer as per #PDHide comments.
Onboard ML From External Repo
[Arguments] ${token}=default
Create Session mxesession ${host}
${data}= Create Dictionary id=${id} author=${author} title=${title} description=${description} version=${version} image=${repo_path} "icon": ${icon_image}
${form_data}= Evaluate {'mldata=': str(${data}).replace("'",'"')}
${header}= Create Dictionary Authorization=${token}
${response}= Post Request session /v1/ml files=${form_data} headers=${header}
Should Be Equal As Strings ${response.status_code} 200
P.S. Thanks #PDHide for your support.
It should be send as a string , and also you don't need equal
${form_data}= Evaluate {'mldata=': str(${data}).replace("'",'"')}
As you observed , we have to pass this as
files=${form_data}
This was what ended working for me. For anyone else with this issue
${data}= Evaluate {'username': (None, 'myusername'), 'password': (None, 'mypwd')}
${response}= Post Request ${Session_id} ${AUTH_TOKEN_URL_PATH} files=${data}
I am trying to load a JSON file and use the values to perform some actions based on my tests. I tried to load the json value which I think I got right, but when trying to log the output, I got error message:
Resolving variable '${qa["REQUEST_ID"]}' failed: TypeError: list indices must be integers or slices, not str
Not exactly sure what this means since I am new to Robot Framework. This is what I did to load and log the values:
${file} Get File ${CURDIR}/RequestIDs.json
${qa} Evaluate json.loads('''${file}''') json
Log To Console ${qa["REQUEST_ID"]}
Json file looks something like:
[
{
"REQUEST_ID" : 10513
},
{
"REQUEST_ID" : 48156
},
{
"REQUEST_ID" : 455131
}
]
So basically I want to get the "REQUEST_ID" value and type that in a text field.
Look at the structure of your json - it's a list of dictionaries; so you have to first specify which list member you want, and then its REQUEST_ID field:
Log To Console ${qa[0]["REQUEST_ID"]
# print the value from all present dictionaries in the list:
FOR ${member} IN #{qa}
Log To Console ${member["REQUEST_ID"]
END
I am trying to enable default MFA for Openstack user using Python keystoneclient API
keystoneclient.users.update
I have sample API curl command from Openstack documentation, where you update the "options" attribute of user account with JSON object
{
"user": {
"options": {
"multi_factor_auth_enabled": true,
"multi_factor_auth_rules": [
["password", "totp"]
]
}
}
}
when I am trying to update the same in Python code I am getting below error
keystoneauth1.exceptions.http.BadRequest: Invalid input for field 'options': u'{ "multi_factor_auth_enabled": true,
"multi_factor_auth_rules": [["password", "totp"]]}' is not of type
'object'
Failed validating 'type' in schema['properties']['options']:
my code is like this
MFA_dict = '{ "multi_factor_auth_enabled": true, "multi_factor_auth_rules": [["password", "totp"]]}'
user = keystone.users.update(user_id, options=MFA_dict)
Never mind I figured it out.
MFA_dict was string, so I had to just remove single quotes and make the object dictionary.
and make lower case true into uppercase True to make it boolean
MFA_dict = { "multi_factor_auth_enabled": True, "multi_factor_auth_rules": [["password", "totp"]]}
i received the following json as response:
{'statusCode': 200, 'Status': 'Success', 'GroupId': None, 'ActivityId': '8234560b-05d8-49a3-92c5-0ae6cea3ff80', 'MessageId': '3c326bb3-9798-4310-b95f-30135441090f', 'MessageType': 2, 'Payload': {'Result': {'Vid': {'Data': [{'label': 'lb1', 'Value': 1, 'Timestamp': '2020-09-01T09:31:30Z'}, {'label': 'lb2', 'Value': 2, 'Timestamp': '2020-09-01T09:31:30Z'}]}}}, 'Priority': 1, 'Time': '2020-09-01T09:37:17.8509538+00:00'}
i extracted "ActivityId" as follows:
${response['ActivityId']}
But how can i extract "Value" from "Data" array where "label" is "lb1"? itried this
${response['Payload']['Result']['Vid']['Data[?(#']['label=='lb1')]']['Value']}
got Syntax Error.
Use the filter script expression of jsonpath ?() to loop through the nodes of array on conditional basis.
${value}= Get Value From Json ${json_object} $.Payload.Result.Vid.Data[?(#.label=='lb1')].Value
log to console ${value}
Note: Put the string after == in single quotes and do not add the space between jsonpath expression
for more details on json path expression have a look at JSONLibrary
Output -