Invalid entity passed while trying below method [duplicate] - eclipse

I am using Karate 0.9.0 version and I want to upload data using csv file. As per new update, it is converting data into JSON. But my API supports csv file format for upload function. How can I upload csv file in post request without converting data into json?
Example
Given path 'xxx/upload'
And header Authorization = xxx
And header Content-Type = 'text/csv'
And request read('classpath:xxx.csv')
When method POST
Then status 202
P.S. This example was working in Karate version: 0.9.0.RC5

Thanks, that is indeed an edge case we hadn't thought of, but you have 2 options that will work nicely:
1) rename your CSV file to *.txt
And request read('classpath:xxx.txt')
2) use the karate.readAsString() API
And request karate.readAsString('classpath:xxx.csv')

Related

How to read actual file contents by name in CSV Data Set config - Jmeter

I was writing JMeter tests for REST API.
Its a post request and we need to send a big xml content in request body.
So I was using CSV Data Set config to parameterize the xml content in body part.
I have created a CSV Data Set config for HTTP Request sampler.
In csv file, I am writing the whole xml content. 1 row for 1 request. It is working fine.
But I found this is bit complex as we have to maintain large lines of xml in csv file.
Is there any way we can write only xml file names or full paths in csv file and CSV DataSet config checks the name and then read the contents of that file and append in request body.
file-abc.xml
file-def.xml
I think this would be easy to maintain as we can have dedicated files for XML content.
Any way to do it using CSV DataSet config?
Or any other way to achieve the same in JMeter tests.
I found this question How to hold Xml file names in CSV Data set Config (Jmeter)
I followed its answer but I am not able to pass the xml content in request body.
Its only passing xmlfile names written in csv file in the request body.
But as per answer it reads the file from xml path/name and pass it in the parameter.
You can keep the file names or paths to the files in the CSV file and read the file content using __FileToString() function directly in the HTTP Request sampler body
If you're keeping XML files in a separate folder you might find Directory Listing Config plugin easier to use in case you want to add/remove/rename files without having to maintain the CSV mapping.
Directory Listing Config plugin can be installed using JMeter Plugins Manager

How to send multiple json body using jmeter?

I have written a REST API and now my requirement is to send the multiple JSON body to the API using POST method from JMeter. I have a csv file with four values(1,2,3,4). And in each of the four files I have the JSON body. I used :
Step-1) added the csv file to jmeter and create a reference and named it JSON_FILE
Step-2) ${__FileToString(C:Path_to_csv_file/${__eval(${JSON_FILE})}.txt,,)}
But from this I am able to access only first file i.e which is named with one. How do I send the body of all file to the API?
Help is highly appreciated.
You won't be able to use CSV Data Set Config as it will read the next value for each thread (virtual user) and/or Thread Group iteration.
If your requirement is to send all the files bodies at once you can go for an alternative approach
Add JSR223 PreProcessor as a child of the HTTP Request sampler which you use for sending the JSON payload
Put the following code into "Script" area:
def builder = new StringBuilder()
new File('/path/to/plans.csv').readLines().each { line ->
builder.append(new File(line).text).append(System.getProperty('line.separator'))
}
sampler.getArguments().removeAllArguments()
sampler.addNonEncodedArgument('', builder.toString(), '')
sampler.setPostBodyRaw(true)
the above code iterates through entries in plans.csv file, reads the file contents into a string and concatenates them altogether. Once done it sets the HTTP Request sampler body data to the generated cumulative string.
Check out The Groovy Templates Cheat Sheet for JMeter to learn more and what else could be achieved using Groovy scripting in JMeter.
Use Body data as follows in HTTP Sampler:
{__FileToString(${JSON_FILE},,)}
You have to put all the file path in your plan.csv file. At each line, there should be a file path.
Example:
Suppose, you have 4 files with JSON body which you want to use in your HTTP sampler.
Give the file path of these 4 files in your CSV file which is plan.csv. Each line contains a file path like this:
/User/file/file1.json
/User/file/file2.json
/User/file/file3.json
/User/file/file4.json
Now, in your CSV data set config, Use the proper file name of CSV file which contains all the file path and give it a variable name like JSON_FILE.
Now, Use {__FileToString(${JSON_FILE},,)} this line in your Body data. Also use the loop count value accordingly.

NetSuite RESTlet output pdf

NetSuite Restlet PDF file encoding issue
The above thread seems to be giving a solution to outputing a pdf with a NetSuite RESTlet. As far as I know, you cannot output a pdf from a restlet, so I'm very confused. I am using a restlet to generate a report and the information ultimately needs to output to a pdf so I was trying to see if there was a work around. I tried the answer code from the above thread and I got the expected error:"error code: INVALID_RETURN_DATA_FORMAT error message:Invalid data format. You should return TEXT."
Am I missing something? Is there a way to export xml to a pdf with a NetSuite RESTlet?
The thread you reference discusses how to generate a PDF file in Netsuite. If you want to return a PDF from a RESTLet you will have to return it as a member of a JSON object. e.g.:
var pdfFile = genPDF(); // base this on the sample
return{
fileName: pdfFile.getName(),
fileContent: nlapiEncrypt(pdfFile.getValue(), 'base64')
};
And then your receiver will have to create the actual file.
Recall that RESTLets are for application-to-system communications. If you are trying to return a PDF to a browser you should probably be using a Suitelet.
If this is part of a larger app and you need the RESTLet then review this post: Save base64 string as PDF at client side with JavaScript for options to display the RESTLet response.
Reading through that answer, it appears you'll need to encode/convert the PDF to string format before returning, so you'll need to use base64 encoding.
The NS method nlapiEncrypt(content, 'base64') seems like it might be a good place to start.
Another avenue to investigate, which I haven't tried, is to first save the PDF in the file cabinet, then to return a public link to that file. You'll need to make sure the file has the correct permissions.

Nancy - return pdf stream via ajax

Is there any sample out there how to return a pdf stream (e.g. generated by Crystal Report) from a nancy module via an ajax request?
I'm not sure what you mean by "ajax request" and why it matters, but recently we had to solve similar problem - i.e. returning pdf, generated from other tool. We ended up using as a template a binary processor, utilizing the content negotiation.
You can modify the binary processor to work with application/pdf MIME type and "pdf" file extensions, so it returns the proper response whenever the request has an Accept header of "application/pdf" or when the request is like http://example.com/reports/report.pdf.
Using this, and assuming you have IReportEngine with Stream GetReportByName(string name), your module will look like (pseudo code):
Get["reports/{reportName}"] = _ => _engine.GetReportByName(_.reportName);

Get the name of an uploaded file

I'm uploading a file using curl:
curl -X POST --data-binary #/home/me/my_file.jpb localhost:9001/upload
And here is how to store it:
def upload = Action(parse.temporaryFile) {
request =>
import java.io.File
val f = new File("tmp/someName") // how do I get the name of the file being uploaded?
request.body.moveTo(f, true)
Ok("File uploaded\n")
}
Note that files can be in any format. I want to get the name of the actually uploaded file. I tried request.body.file.getName but it returns gibberish.
How do I do that?
I am fairly certain you cannot get the file name from the binary stream you are uploading via curl. You need to explicitly provide the file name separately.
The options I can think of are these:
If your Content-Type header is instead multipart/form-data, then the process is quite simple as described here
Upload JSON with a String for the file name and a binary portion for the file.