How do I send Request Body in Robot Framework
Post request body but it gives Issue as Client Error: “Not Acceptable for url: https://apiexample.com/demo”
*** Test Cases ***
GenerateToken
${headers}= create dictionary Content-Type=application/json
${body}= create dictionary username=abc#example.in password=abc123 // I can not pass this request body in Post session below
#${body}= get file /Users/Documents/data.json // This method works but **I don't want to use this file method**
create session mysession ${base_Url} headers=${headers}
${response}= Post On Session mysession /demo data=${body} headers=${headers}
Try with
*** Test Cases ***
GenerateToken
${headers}= create dictionary Content-Type=application/json
${body}= create dictionary username=abc#example.in password=abc123
create session mysession ${base_Url} headers=${headers}
${response}= Post On Session mysession /demo json=${body} headers=${headers}
Adding conversion to string for the message body helped for me. Like this:
*** Test Cases ***
GenerateToken
${headers}= create dictionary Content-Type=application/json
${json_obj}= create dictionary username=abc#example.in password=abc123
${body}= Convert JSON To String ${json_obj}
create session mysession ${base_Url} headers=${headers}
${response}= Post On Session mysession /demo json=${body} headers=${headers}
Related
I have a multi object request which look like follow in postman
{ "affordability": {
"grossIncome": 100000,
"netIncome": 80000,
"capitalRequired": 25000,
"groceries": 200,
"utilities": 300,
"savings": 400,
"services": 500,
"transport": 600,
"support": 800,
"housing": 700,
"other": 900
},
"employment": {
"sector": "CentralGovernmentSocial",
"status": "Contract",
"startDate": "2010-08-01",
"endDate": "2030-08-01"
},
"declarations": {
"debtLiability": "None",
"pendingRetrenchment": "false",
"knownMedicalCondition": "false"
},
"bank": "ABSA"
}
I am doing some automation on our API's and this one got me. I have struggled for quite some time to get the request to look like the postman request. I have managed to get that right. Though I am not entirely sure if the method I went about to solve the problem is allowing for the POST request. Cause when I run the POST request I get the below error.
HTTPError: 400 Client Error: Bad Request for url: this is a url
This is what my code looks like in:
# &{baseURL}= this is a url
# ${offer}= /offer
# ${URL}= Catenate This is a url ${appID} /offer
# log to console ${URL}
# Create Session httpbin this is a url
&{affordability}= Create Dictionary grossIncome=60000 netIncome=30000 capitalRequired=25000 groceries=500 utilities=400 savings=300 services=200 transport=100 support=100 housing=500 other=100
${affordabilityobject}= Catenate {'affordability': ${affordability}
# log to console ${affordabilityobject}
&{employment}= Create Dictionary sector=CentralGovernmentSocial status=Contract startDate=2019-08-01 endDate=2023-08-01
${employmentobject}= Catenate 'employment': ${employment}
# log to console ${employmentobject}
&{declarations}= Create Dictionary debtLiability=None pendingRetrenchment=false knownMedicalCondition=false
${declarationsobject}= Catenate 'declarations': ${declarations}
# log to console ${declarationsobject}
# &{bank}= Create Dictionary bank=FirstNationalBank
${bank}= Catenate 'bank':'FirstNationalBank'}
log to console ${bank}
${fullrequest}= Catenate SEPARATOR=, ${affordabilityobject} ${employmentobject} ${declarationsobject} ${bank}
log to console ${fullrequest}
${resp}= Post this is a url/${appID}/offer json=${fullrequest}
Don't use Catenate for nested dicts,
You can use Create Dictionary like this :
Then, I'm not sure, but I think you need to transform the dict to json with :
${json_string}= evaluate json.dumps(${json}) json
&{baseURL}= this is a url
${offer}= /offer
${URL}= Catenate This is a url ${appID} /offer
log to console ${URL}
Create Session httpbin this is a url
&{affordability}= Create Dictionary grossIncome=60000 netIncome=30000 capitalRequired=25000 groceries=500 utilities=400 savings=300 services=200 transport=100 support=100 housing=500 other=100
&{employment}= Create Dictionary sector=CentralGovernmentSocial status=Contract startDate=2019-08-01 endDate=2023-08-01
&{declarations}= Create Dictionary debtLiability=None pendingRetrenchment=false knownMedicalCondition=false
&{fullrequest}= Create Dictionary affordability=${affordability} employment=${employment} declarations=${declarations} bank=FirstNationalBank
log to console ${fullrequest}
${fullrequestJson}= evaluate json.dumps(${fullrequest}) json
${resp}= Post this is a url/${appID}/offer json=${fullrequestJson}
A better way to do it, is to store your json object in a separate file like "myjsonToPost.json" and you load it from file like this :
*** Settings ***
Library JSONLibrary
*** Keywords ***
&{baseURL}= this is a url
${offer}= /offer
${URL}= Catenate This is a url ${appID} /offer
log to console ${URL}
Create Session httpbin this is a url
${fullrequest} Load Json From File file_name=${EXECDIR}/path/to/myjsonToPost.json
# This method, without Library JSONLibrary
# ${fullrequestJson} evaluate json.dumps(${fullrequest}) json
# Or this one, with JSONLibrary
${fullrequestJson} Convert String To Json ${fullrequest}
${resp} Post this is a url/${appID}/offer json=${fullrequestJson}
Post Request Create Digital Offer
Create Session httpbin this is a url
&{affordability}= Create Dictionary grossIncome=${60000} netIncome=${30000} capitalRequired=${25000} groceries=${500} utilities=${400} savings=${300} services=${200} transport=${100} support=${100} housing=${500} other=${100}
&{employment}= Create Dictionary sector=CentralGovernmentSocial status=Contract startDate=2019-08-01 endDate=2025-08-01
&{declarations}= Create Dictionary debtLiability=None pendingRetrenchment=false knownMedicalCondition=false
&{fullrequest}= Create Dictionary affordability=${affordability} employment=${employment} declarations=${declarations} bank=ABSA
${CreateOffer_response}= Post this is a url/${appID}/offer json=${fullrequest}
Should Be Equal As Strings ${CreateOffer_response.status_code} 201
When testing some api, the token need to be generated each 3min and I would like to do the refresh automatically, I did the following
I have a collection "CollectionGetter" which contain some requests
from "CollectionGetter" collection :
I've added the following script in "Tests" tab
var jsonData = pm.response.json();
pm.environment.set('getToken', jsonData.access_token);
on authorozation tab,set :
Type = Bearer token
Token {{getToken}}
then selected a request under CollectionGetter :
getAccount (GET url/api/account)
Auth = inherit autho from parent
and sent it
=> got a 401 JSONError: No data, empty input at 1:1
Any help ?
is my configuration correct
I am trying to make a simplified version of test report where I am generating a single HTML file report containing only assertion and error response message when there is any (attempting to not publish all the logs and steps).
I understand that we have hooks in karate. However I have looked for karate objects in the github but unable to found any objects where I can extract the response from (to be passed to the js function called on hook)
What I am doing right now is this:
Config:
//karate-config.js
karate.configure('afterScenario', karate.call('classpath:hooks.js'));
Hook:
//hooks.js
//Looking on how to extract the response and log it here
function(){
var info = karate.tags;
karate.log('Tags', info);
}
Am I missing anything on the karate objects? Or this should be achieved in another way?
Thanks a lot!
Try this:
var response = karate.get('response');
EDIT better example:
Background:
* configure afterScenario = function(){ karate.log('***', karate.get("response.headers['X-Karate']")) }
Scenario:
Given url 'http://httpbin.org'
And path 'headers'
And header X-Karate = 'test'
When method get
# this will fail
Then status 400
I have tried with both karate.get('response') and response directly, and both work. If you use karate.call() pass the response as a parameter.
How can we get response body from DataRequest object? I am using RxAlomafire.request method for making http calls. Need some data from response body when http request fails.
I have a REST request that respond with the following:
{
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IlQwWE8xNnAtMmZzMWxremV5",
"expires_in": 2592000,
"token_type": "Bearer"
}
I want to take the value of access_token, store it in a property and reuse it for two subsequent requests.
Following some tutorial here, when running the request that obtains the access_token I get a:
error parsing target property: error unexpected element CDATA
But why?
There is no CDATA in my raw response.
If you've problems using transfer properties step to get the JSON value from your response, you can use a groovy test step to achieve your goal.
So create a groovy test step to parse your response, get your value and set it as a property (for example at testCase level) with the follow code:
import groovy.json.JsonSlurper
// get response using the name of your test step
def response = context.expand('${REST Test Request#Response}')
// parse response
def jsonResp = new JsonSlurper().parseText(response)
// get the token an set as a property in the testCase
testRunner.testCase.setPropertyValue('access_token',jsonResp.access_token)
Then in the other testSteps (REST or SOAP...) you can use the follow code to get the access_token value you set in the testCase:
${#TestCase#access_token}
Hope this helps,