Quick Karate POST request advice [duplicate] - karate

This question already has an answer here:
Passing JSON request starting with the character [ gives the error: invalid request format with GET operation [duplicate]
(1 answer)
Closed 2 years ago.
I'm attempting to send a simple POST request and struggling slightly.
Postman data request shown
--data-raw '""'
Given url TESTURL
And request '""'
When method post
Then status 200
A 500 response is returned every time, this works in postman. What am I missing?

Try adding a header
* header Content-Type = 'text/plain'

Related

How to get REST response size in Karate? [duplicate]

This question already has an answer here:
Karate: Is there a way of getting JSON object keys length from an Examples variable?
(1 answer)
Closed 1 year ago.
In Karate framework using Visual studio, I am making a REST call. I need to get the response size in KB or MB.
I tried these:
print response.size
Response: [jdk.dynalink.beans.SimpleDynamicMethod int java.util.HashMap.size()]
print response.length
Response: 6
This is the # of nodes of the response (items,hasMore,count,limit,offset,links)
print responseHeaders
Response: This gives me the response headers. It does not have response size
Can someone suggest a way to get the response size?
The responseBytes will contain the raw bytes and you can get the JavaScript length property like this:
* print 'size:', responseBytes.length
Note that I am using Karate version 1.X

How do I pass apikey as authorization in a POST/GET request in Karate Framework feature file [duplicate]

This question already has an answer here:
Is there a way to update the headers in one feature file and use the Auth token from Karate.config.js?
(1 answer)
Closed 1 year ago.
This is the first time I am trying my hands at Karate for my work. I have a feature file that is a GET request and uses an authorization method as an apikey but when I am passing an authorization as a header its giving me an error
"required (...)+ loop did not match anything at input 'Scenario:'"
Below is my feature file for reference
Feature: Get Profile
Background:
* url 'https://csXXX-XXXX.XXXXXXXXX.net'
* header Accept = 'application/json'
* header Authorization = 'apikey XXXX-XXXX-XXXX-XXXX'
Scenario: Profile GET on an id
Given path '/v1/Profile'
And param idProfile='XXXX'
When method get
Then status 200
Any help is appreciated.
Looks like you missed the Feature: at the top of the file.
I suggest you use the quickstart or ZIP release, so you get a ready to use test to try: https://github.com/intuit/karate#getting-started

in Karate DSL, is there a way to have redirects perform a POST request instead of a GET request? [duplicate]

This question already has an answer here:
Post method gets converted to GET after redirection
(1 answer)
Closed 2 years ago.
I have the following Karate script that by default has redirects turned on.
Scenario: First Test
Given path 'somePath'
And request ''
And header Content-Type = 'text/html'
And param _csrf = csrf
And param username = 'username'
And param password = 'password'
When method post
Then status 200
The issue is after getting a 302 from the API, the next request automatically submits a GET request. I would like it to submit a POST request instead.
in cURL, there is an existing parameter that allows users to do that. see below.
--post302 Do not switch to GET after following a 302
is there anyway to do that in Karate DSL?
Yes please read the docs for configure folowRedirects. There is also an example on how to read the Location response header to manually make the request you want.
Scenario: get redirects can be disabled
* configure followRedirects = false
Given path 'redirect'
When method get
Then status 302
And match header Location == demoBaseUrl + '/search'
* def location = responseHeaders['Location'][0]
Given url location

Validating API response in Karate framework [duplicate]

This question already has answers here:
Can we parameterize the request file name to the Read method in Karate?
(2 answers)
Closed 1 year ago.
I want help for validating API response using karate framework.
I have API’s which are “Independent” on each other.
I have POST method along with request parameters. when I hit that particular API got the response with different parameters (no single match from request parameter and response parameter).
Now I want to validate response parameter value.
example : request: “method” post
school name: “abcd”
register date : “1:10:2010″
Response:
Principle name : ” pqrs”
Principle Email id “pqrs#gmail.com
now I want to validate that “principle name ” should not be null
I have implemented like this but it it doesn’t work
Feature: School info
Background:
* url baseUrl
Scenario: check Principles info
Given path ‘School info’
And request {school name: “abcd” ,register date : “1:10:2010”}
When method post
Then status 200
And match response.response contains {“type”: “Success”,”code”:20000}
And match response.principle list[*] { “Principle name”: “#notnull”}
whenever I run this file it always passes the API wvwnt if the Principle name filed is null.
It just checks the success message (And match response.response contains {“type”: “Success”, ”code”:20000}) and pass the API
your code for validating principlelist doesn't have proper assertions.
match each will be more convenient for validating json array with a schema
* match each response.principlelist contains {"Principal name" : "#notnull"}

Request mandatory for POST method call in Karate

Today I came across a scenario where there was a POST method call but that does not require a request data and query parameters are sufficient. But in Karate framework it is mandatory to give request data when it is POST method. So I had to provide request as 'null' explicitly. Is there any way in Karate that if there is no request data then you can skip providing request data as 'null'.
This is what I usually do if there's no request body :
Given path '/path/to//action'
And request ''
When method post
Then status 200
(Haven't found a way to skip the request step)
I tried this way:
Given url 'url'
And params param_value
And request '{}'
When method post
Then status 200
Its same as mentioned above just I gave empty request body