dynamically update the request json and send it as multipart form data in karate [duplicate] - json

In my karate tests i need to write response id's to txt files (or any other file format such as JSON), was wondering if it has any capability to do this, I haven't seen otherwise in the documentation. In the case of no, is there a simple JavaScript function to do so?

Try the karate.write(value, filename) API but we don't encourage it. Also the file will be written only to the current "build" directory which will be target for Maven projects / stand-alone JAR.
value can be any data-type, and Karate will write the bytes (or plain-text) out. There is no built-in support for any other format.
Here is an example.
EDIT: for others coming across this answer in the future the right thing to do is:
don't write files in the first place, you never need to do this, and this question is typically asked by inexperienced folks who for some reason think that the only way to "save" a response before validation is to write it to a file. No, please don't waste your time - and please just match against the response. You can save it (or parts of it) to variables while you make other HTTP requests. And do not write your tests so that scenarios (or features) depend on other scenarios, this is a very bad practice. Also note that by default, Karate will dump all HTTP requests and responses in the log file (typically in target/karate.log) and also in the HTML report.
see if karate.write() works for you as per this answer
write a custom Java (or JS function that uses the JVM) to do what you want using Java interop
Also note that you can use karate.toCsv() to convert JSON into CSV if needed.

My justification for writing to a file is a different one. I am using karate explicitly to implement a mock. I want to expose an endpoint wherein the upstream system will send some basic data through json payload using POST/PUT method and karate will construct the subsequent payload file and stores it the specific folder, and this newly created payload file will be exposed through another GET call.

Related

Caching API response from simple ruby script

I'm working on a simple ruby script with cli that will allow me to browse certain statistics inside the terminal.
I'm using API from the following website: https://worldcup.sfg.io/matches
require 'httparty'
url = "https://worldcup.sfg.io/matches"
response = HTTParty.get(url)
I have to goals in mind. First is to somehow save the JSON response (I'm not using a database) so I can avoid unnecessary requests. Second is to check if the new data is available, and if it is, to override the previously saved response.
What's the best way to go about this?
... with cli ...
So caching in memory is likely not available to you. In this case you can save the response to a file on disk.
Second is to check if the new data is available, and if it is, to override the previously saved response.
The thing is, how can you check if new data is available without doing a request for the data? Not possible (given the information you provided). So you can simply keep fetching data every 5 minutes or so and updating your local file.

Validate request payloads and responses against a given OpenApi/Swagger specification

We have an HTTP API which is written with the hug framework for Python but this is not set in stone and could be replaced with something else in the future.
Next to it, we have manually written an OpenApi/Swagger 2.0 specification file in YAML for that API in order to provide interactive documentation. However, both are not connected to each other by any of the tooling from the Swagger ecosystem - when we make changes in the API, we make the same changes in the specification and vice versa (which is not ideal but we might improve on this later).
Now, for the sake of integration testing, we would like to take a few static requests and their payloads (JSON and multipart/form-data) as well es responses (JSON) and validate if they fit the models from our specification file.
What could be an easy way to achieve this? I am open to any technology or programming language as long as complexity stays reasonable.
For Open Api 3 documents, my colleagues and I had the same requirement and ended up writing our own command-line tooling to achieve this, I've open sourced it here: https://github.com/hevans90/oa3-api-defender
It will dynamically call any endpoints specified in the paths array of a specification, currently supporting GET, DELETE, POST, PATCH, PUT requests. It will dynamically create requests based on example values provided in the spec.
Query & Path example parameter values will be included in generated requests. Request bodies (in POSTs for example) are dynamically generated from example values in the requestBody object, in the spec.
To provide valid examples in your spec, see this article

HTTP request from XQuery code inside MarkLogic pipe

I want to send text using POST method to another HTTP server and receive a JSON file in response from XQuery code of pipe in MarkLogic. The idea is whenever XML or JSON documents are inserted into MarkLogic, it triggers a pipe to read it and sends one element to another web server; in my situation, I want to send to Rosoka server to do natural language processing, after that I want to store the returned data "it is json file" in MarkLogic.
I appreciate if you could help.
marklogic only mentioned that is possible but no further help
There are many ways to do this.
A start is to look at "Triggers"
https://docs.marklogic.com/guide/app-dev/triggers
There is also Content Processing Framework ( "CPF" which is a higher level workflow based on triggers).
https://docs.marklogic.com/guide/cpf
if you read those guides you should be able to ask more specific questions if needed.

How to transfer large xml file and zip file via rest api?

I have an application which uploads a large file in the XML format and sometimes a zip file. Now I want to have that file transferred to other application via REST API. I am thinking to pass the binary data in to json response.
I have the following questions for my approach.
Is sending binary in json the best approach/practice to do it?
Will this be PUT scenario as receiver application doesn't know about new uploaded file?
If that makes it easier for the second service to consume it, I see no problem with it. You can send it on any format you want, as long as it's accepted and you're setting the Content-Type and Accept headers properly.
You use a PUT only when you're sending a complete replacement of the resource at the target URI. If you know the final URI for that and if a GET to the same URI right after the PUT will retrieve as response the same body you just submitted, it makes sense to use PUT, otherwise, use a POST.

How to change content of Post Body in JMeter HTTP Request

Please forgive my ignorance as I'm a jmeter noob. My webservice accepts JSON objects so I was able to write a rudimentary test where I create an HTTP Request with a JSON object in the "Post Body" portion of the http request.
Anyway, what I want to do is have the HTTP Request choose a different JSON object from a csv file or some other input mechanism so that I can randomize the types of queries that are being run during the load test. Is there a way to do this? The closest is probably using variables (section 4.11 in the user manual) but I have a feeling that's not how variables are used.
A second way I've theorized (although I haven't tried yet since I think the method above is easier) is to create a HTTP Request Default obj with a bunch of HTTP Requests with different JSON objects in them and then use a Random Controller to randomly go thru my multiple HTTP Requests on each pass.
If there's a third way, I'm all ears to learn how to use this tool. I'll continue to read and possibly experiment with plan B above. Thanks in advance for any help you can give me.
UPDATE: So I tried the second way and it seems to work. I had 3 different HTTP requests and the number of times each request gets hit varies from run to run. I still invite answers from the community since I'd like to see what the pros do for issues similar to mine.
You have partially answered your question yourself, by saying "csv file or". Here are the specifics.
You will have to use CSV data set config in your test plan to read data from CSV. In your post body, use the variables read from CSV.
Here is a screen cast showing how to use csv data set config.