Karate send json body with GET Method [duplicate] - karate

This question already has an answer here:
Karate API : How to hit an endpoint url with post method which does not have request body
(1 answer)
Closed 1 year ago.
How to provide request body for GET method using Karate API.
When trying to provide Request body for Get method in Karate API, it doesnt take the request and throws 500 status code.

As stated in HTTP GET with request body
using the request body within a GET request to change the semantics of the request is highly discouraged.
It's debatable that Karate ignores the message body when using method GET and if you think there is a use case for that feature your free to file an issue.
Furthermore, the 500 status code you observe is returned by your system under test because of the missing message body and has nothing to do with karate.
EDIT: confirmed that Karate 1.0 will support a body along with a GET

Related

Error- Validate Multiple APi call response in one scenario in karate

I am trying to use two api call (Post & put call) in one scenario in karate framework. But I am not able to validate 2nd Api call response. when i tried to do like response.id , it passing 1 st api call response.
Is there any solution for it?
In your feature file, you wrote your print response in When instead of writing it after Then. Which means it is showing the previous response only. As per documentation response gets overridden after making a new http request.
Following,
...
When method put
And print 'jokerresult-->',response.joker_result
Then status 201
Should be like,
...
When method put
Then status 201
And print 'jokerresult-->',response.joker_result
let me know if it worked for you or not.

How do we test a GET call with POST body in its request using karate [duplicate]

This question already has an answer here:
How to provide JSON request body for GET method in Karate API [duplicate]
(1 answer)
Closed 1 year ago.
How do we test a GET call with POST body in its request using karate API.
When trying to provide Request body for Get method in Karate API, it doesn't take the request and throws 400 status code and says expected JSONArray and found Null
A body for a GET is supported in the latest versions. See https://stackoverflow.com/a/55812032/143475
Else your question does not make sense, so please consider this not supported in Karate.

How to provide JSON request body for GET method in Karate API [duplicate]

This question already has an answer here:
Karate API : How to hit an endpoint url with post method which does not have request body
(1 answer)
Closed 1 year ago.
How to provide request body for GET method using Karate API.
When trying to provide Request body for Get method in Karate API, it doesnt take the request and throws 500 status code.
As stated in HTTP GET with request body
using the request body within a GET request to change the semantics of the request is highly discouraged.
It's debatable that Karate ignores the message body when using method GET and if you think there is a use case for that feature your free to file an issue.
Furthermore, the 500 status code you observe is returned by your system under test because of the missing message body and has nothing to do with karate.
EDIT: confirmed that Karate 1.0 will support a body along with a GET

How to use a Postman Mock Server

I have followed the guide here to create a postman mock for a postman collection. The mock seem to be successfully created, but I have no idea how to use the mock service.
I've been given a url for the mock, but how do I specify one of my requests? If I issue a GET request to https://{{mockid}}.mock.pstmn.io I get the following response:
{
"error": {
"name": "mockRequestNotFoundError",
"message": "We were unable to find any matching requests for the mock path (i.e. undefined) in your collection."
}
}
According to the same guide mentioned above the following url to "run the mock" https://{{mockId}}.mock.pstmn.io/{{mockPath}} but what exactly is mockPath?
Within my collection I have plenty of folders, and inside one of these folders I have a request with an example response. How do I access this example response through the mock? Thanks for all help in advance!
Here's the Postman Pro API, which doesnt mention a lot more than just creating reading mocks.
I had the same issue seeing an irrelevant error but finally I found the solution. Unfortunately I cannot find a reference in Postman website. But here is my solution:
When you create a Mock server you define your first request (like GET api/v1/about). So the Mock server will be created but even when you obtain your API key and put it in the header of request (as x-api-key) it still returns an error. It doesn't make sense but it turned out that defining the request is not enough. For me it only started returning a response when I added an Example for the request.
So I suggest for each request that you create, also create at least one example. The request you send will be matched with the examples you have created and the matched response will be returned. You can define body, headers and the HTTP status code of the example response..
I have no Pro Postman subscription and it worked for me using my free subscription.
Menu for adding an example or selecting one of them for editing:
UI for defining the example (See body, headers and status) :
How to go back to the request page:
Here is the correct reply I get based on my example:
If you request in the example is a GET on api.domain.com/api/foo then the mockPath is /api/foo and your mock endpoint is a GET call to https://{{mockid}}.mock.pstmn.io/api/foo.
The HTTP request methods and the the pathname as shown in the image below constitute a mock.
For ease of use the mock server is designed to be used on top of collections. The request in the examples is used as is along with response attached to it. The name of the folder or collection is not a part of the pathname and is not factored in anywhere when using a mock. Mocking a collection means mocking all the examples in within your collection. An example is a tuple of request and response.
An optional response status code if specified lets you fetch the appropriate response for the same path. This can be specified with the x-mock-response-code header. So passing x-mock-response-code as 404 will return the example that matches the pathname and has a response with status code of 404.
Currently if there are examples with the same path but different domains, and mock is unable to distinguish between them it will deterministically return the first one.
Also if you have several examples for the same query :
Mock request accept another optional header, x-mock-response-code, which specifies which integer response code your returned response should match. For example, 500 will return only a 500 response. If this header is not provided, the closest match of any response code will be returned.
Optional headers like x-mock-response-name or x-mock-response-id allow you to further specify the exact response you want by the name or by the uid of the saved example respectively.
Here's the documentation for more details.
{{mockPath}} is simply the path for your request. You should start by adding an example for any of your requests.
Example:
Request: https://www.google.com/path/to/my/api
After adding your mock server, you can access your examples at:
https://{{mockId}}.mock.pstmn.io/path/to/my/api

REST API request failed http response code

I am implementing a REST API and i have a set_ftp_credentials request
for example
POST api.domain.com/object/set_ftp_credentials
This request checks that the ftp credentials are good by trying to connect.
We had a discussion here whether we should return a HTTP response 200 and in the content
return the the request have failed and the reason.
Or we should return some other HTTP response code (40* response code).
What is considered the right way of doing this ?
And if not 200 then what do you think is the right response code ?
I believe that 200 is an OK response and should only be sent when the request was successfully satisfied.
One approach would be to answer the following questions for yourself:
What HTTP methods are you supporting?
What is the expected behavior of each HTTP method?
For each supported HTTP method, What are the possible classes of failures that can result if something goes wrong?
Now, return your expected response pay load with a 200 HTTP response code. Return client side error (400 class) or server side error (500 class) for the failures.
You might want to consider the following:
Return error codes that are specific to your application and map them to HTTP error codes
Have separate data contracts (assuming JSON format or XML schema) defined for successful response and error responses