Getting 202 instead of 200 using Rest assured or Robot Framework - api

I have performed a GET request as follows:
TEST1 : Using Rest assured:
RestAssured.baseURI = my_BaseUri;
Response response = given().header("x-ibm-client-id", my_XibmClientId).auth().preemptive()
.oauth2(my_accessToken)
.accept(ContentType.JSON).when()
.get(my_BasePath + "/" + my_processId)
.then().log().all()
.extract().response();
TEST2 : Using Robot Framework:
${headers}= Create Dictionary x-ibm-client-id=${my_xIbmClientId} Authorization=Bearer ${my_AccessToken} Accept=application/json
${response}= Get Request mysession ${my_BasePath}/${my_processId} headers=${headers}
So the issue is: I always get a status 202 (accepted) instead of 200. However, it works properly with the POSTMAN (the response status is 200).
Thanks for your help.

Try using this:
${headers}= Create Dictionary x-ibm-client-id=${my_xIbmClientId}
Authorization=Bearer ${my_AccessToken} Content-Type=text/plain
${response}= Get Request mysession ${my_BasePath}/${my_processId}
headers=${headers} alias=API
I just added content-type= text/plain

Related

API Post request with RobotFramework - Empty Param

I have the following POST request to configure in RobotFramework,
trouble is that the devs set up the request as presented in the screenshot.
This is a file id which you get from a file manager and download it.
My question is since there isn't any params, key, value like FileID etc.
What would be your best bet to send that request successfully via RobotFramework?
[screenshot][1]
The code that I have tried is:
Create Session mysession url=${test_env} verify=true
&{body}= Create Dictionary id=d67b39a6-4ea9-497f-a653-5eb2da418d23
&{header}= Create Dictionary Cache-Control=no-cache
${response}= POST On Session mysession /download data=${body} headers=${headers1}
Status Should Be OK ${response} #Check Status as OK```
Response I receive is Bad Request or Unsupported media
[1]: https://i.stack.imgur.com/kGyHo.png

How to solve HTTPError: 400 Client Error: Bad Request for URL inRobot Frame work

I know there are many similar kinds of questions available but none of them worked.
Can someone tell me if there is any kind of syntax error for the Testcase below
Create Token
Create Session testsession ${baseUrl} verify=true
${body}= create dictionary clientId=unittest.cc.client clientSecret=RyDQ$xxxxxRtv
${header}= create dictionary Content-Type=application/json
${resp}= POST On Session testsession ${reqUri} json=${body} headers=${header} params=${ApiKeyParameter}
${source data}= Evaluate json.loads("""${resp.content}""") json
${token}= Set Variable ${source data['accessToken']}
#No errors Uptill this much - Bearer token creation was successful after that getting error while using it
${header}= create dictionary Authorization=${tpre} ${token} Content-Type=application/json cookies=ss-id=KF84fFy4txxxxxxxxx76i; ss-pid=StDTDxxxxxxxxxxxxn7r
${body}= get file API/data.txt
log to console ${header}
${resp}= post on session testsession /orders json=${body} headers=${header}
log to console ${resp.status_code}
The problem is every time I run the test I am getting a 400 error. Below is the Python code provided by POSTMAN and the screenshots of the headers used. Now I am not sure of how to get the HOST header in my python or maybe robot framework.
Please let me know if any additional details are needed. I am not sure of headers in the URL formation while get or post request is done
Is there any way to find that out?
import requests
import JSON
url = "https://domain:10001/orders?format=json"
payload = json.dumps({ Can ignore this part
})
headers = {
'Authorization': 'Bearer xxx',
'Content-Type': 'application/json',
'Cookie': 'ss-id=xxx; ss-pid=xxx'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
If you all ever come across this kind of issue don't forget to check the body of the JSON / XML you are sending.
Mine resolved as I was saving the dump JSON in a text file so while reading from the file my code was adding some extra spaces in front so I was getting a 400 error.
For further information try logging the Response Content it must show you the error message.

Karate API : How to hit an endpoint url with post method which does not have request body

I am getting below error when I am trying hit a endpoint url with post method which does not have a body. In postman I am able to hit endpoint url with post method without body. I tried all steps by referring the Karate API docs.
**Error** : com.intuit.karate.exception.KarateException: TestScenarios.feature:56 -
request body is required for a POST, please use the 'request' keyword
testurl is :
**test-api.test.com/test-com/api/check/initiate?
lang=en&cntCode=us&id=8d1b9355**
Attempt 1:
Given url 'test-api.test.com/test-com/api/check/initiate?lang=en&cntCode=us&id=8d1b9355'
When method post
Then status 200
* print response
Attempt 2:
Given url testurl
And param lang= en
And param cCode = us
And param id= '8d1b9355'
When method post
* print 'Response'+response
Attempt 3:
Given url testurl
And form field lang= en
And form field cCode = us
When method post
Then status 200
Can someone help me to understand the issue and wrong in my approach.
Two possible options depending on your server:
* request {}
Or as per the docs:
* request ''

karate | xml post method exeuction

I’m having issue with xml post request where post method is not executed. When I try to post same request body in post man it worked.My test is success with 200 but actual request is not executed.
Please let me know if I’m missing
To pass the request body,I’m calling through java object and payload is correctly constructed and printed.In execution test is success and doesn’t print response.But actually test is not executed.
Only headers are printed.
***************** create-user.feature*****************
Feature: create ims user for provided country
Requires country code,
Background:
# load secrets from json
* def createuser = Java.type('com.user.JavaTestData')
* def create = createuser.createUser("US")
Scenario: get service token
Given url imscreateuserurl
And request create
When method post
Then status 200
* print response
***************** create-user.feature*****************
Here is java class
public class JavaTestData {
private static final Logger logger = LoggerFactory.getLogger(JavaTestData.class);
public static String createUser(String countryCodeInput) {
logger.debug("create user for country code input", countryCodeInput);
Unless you post a full working example, no one can help you. Pretty clear that the value of create is null or empty.
Also I personally think you are wasting your time using Java. The whole point of Karate is to avoid using Java as far as possible.
Look at these examples for ideas: https://github.com/intuit/karate/blob/master/karate-junit4/src/test/java/com/intuit/karate/junit4/xml/xml.feature
Edit: also refer to the doc on type-conversion: https://github.com/intuit/karate#type-conversion
#Peter, here is my feature file
Feature: create ims user for provided country
Requires country code,
Background:
# load secrets from json
* def createuser = Java.type('com.adobe.imscreateuser.JavaTestData')
* def create = createuser.createUser("US")
Scenario: get service token
Given url imscreateuserurl
And header Content-Type = 'application/xml; charset=utf-8'
And request create
When method post
Then status 200
* print response
I have performed print for create and showing complete payload.At when method post -> statement its going as null or empty...
Not sure where it is missing

Scope of any element/object of a response json is within the scenario only

Scenario: Generenate jwt and check status
Given path '/sdk/jwt'
header Authorization = call read('jwt.js') { token: 'e68c82a665847c', secret: 'f08f06f1f41f4479854c' }
When method get
Then status 200
And def tkn = response
Scenario: Get project meta info for an instance
Given path '/meta/project'
And header Authorization = JWT tkn #which the response of 1st scenario.
When method get
Then the is status 200
But tkn value is not coming in the second scenario.
Define in background, it will work as before test