Get the name of an uploaded file - scala

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.

Related

Invalid entity passed while trying below method [duplicate]

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')

Getting pdf data from cloudinary and saving it in flutter

I have been using cloudinary to store my pdf files the use firebase as my backend. However, am having this issue with it. When I use http.get on the link provided by cloudinary https://res.cloudinary.com/cloudname/raw/upload/vxxxxxxxxxxx/xxxxxxxxxxxxxxxxxxxx I get ascii data instead of the pdf file. I have tried to write the data as string to a file with .pdf extension that has not worked. I have tried to convert it to bytes then write to the file as bytes and that has not worked either. Any help on how I can save this file on android using flutter It would be a great help.
await http.post('https://api.cloudinary.com/v1_1/${DotEnv().env['cloud_name']}/raw/upload',body: { "file": "data:raw/pdf;base64,$pdf", "upload_preset": DotEnv().env['upload_preset']} );
var response = await http.get('https://res.cloudinary.com/<cloudname>/raw/upload/v1590958359/gsvxe4zp7bb6yyldrccu');
final directory = await getApplicationDocumentsDirectory();
final path2 = directory.path;
File x = File('$path2/trial.pdf');
x.writeAsString(response.body);
Here is the code as I currently have it. pdf in this case is the base64 encoding of the pdf. The link I have here in http.get is from a file I had already uploaded.
Thanks for adding the additional details.
Firstly, I would recommend you change the resource_type from raw to image. In addition, please update the MIME type in your Base64 Data URI to application/pdf.
The URL you then get from Cloudinary upon a successful upload will be to a PDF resource which means you can download it with a simple cURL request or via any HTTP library.
For example, using the secure_url to the asset returned in the Upload API Response to download the file to my_test_file.pdf -
curl -s https://res.cloudinary.com/demo/image/upload/multi_page_pdf.pdf --output my_test_file.pdf

Soap message encoded in base64 binary data multiple files

Recently I got a new project to work on a SOAP service and to Get and Post messages to a ASP.NET service based on xml.
The issue is that I managed to make the soap request and get the message.
The message looks like this:
UEsDBBQAAAAIAAdUe06+NXE0kR4AADLSAQALAAAAUHJvZHVzZS54bWzUXW1z48YN5k/h5EMnmbMsvomSpmkzFCXbjERJoSTb52/p9dq5mbxN2svczy/........
The message is Base64 Binary on RFC 4648 with multiple xml documents on it.
How I can construct this documents from the code in php?
The documents encrypted in this request are 3 xml files.
I managed to get them from an online decryptor called freeformatter with download function.
If I try to decode the result I get something like:
PKT{N�5q4�2�Produse.xml�]ms��
�O��C'��,����i3%یDI�$��o��ڹ��M����/�,��|vL�O�$�/�xv,,�u�s>9?;?....
Is there a solution for this?
I'm new to SOAP so I don't understand too much of it.
Thank you but i mannged to solve it.
I gonna post here the sollution so everyone who facing the same issue, get the response.
The first thing you need to do when you have an .zip file in a base64 binary string is to catch the response to a txt file.
Let's say the response from soap it's called ' $response ' and we need to catch this to an file. We do like this :
$response = $client -> _getLastResponse();
$fille = "response.xml";
fille_put_contents($fille,$response);
Now we got the response to an xml file.
The next thing to do is to get the response from xml values.
Lets say our value is <ResponseFromServer> .
`$b64 = "b64.txt";
$dom = new DomDocument();
$dom = load("response.xml");
$data = $dom->getElementByTagName("ResponseFromServer");
$catchb64 = $data;
fille_put_content($b64,$catchb64);`
Now we got the clean Base64 Binary string in one fille.
The next thing we need is to create the document ( in this case is a .zip fille)
`$input_fille = "response.txt"; // the fille with clean base64 binary data on it
$output_fille = "result.zip"; //the fille we need to create on system with the
documents decrypted
$content = fille_get_contents($input_fille); // Reading the content of input fille
$binary = base64_decode($content); // Decoding to binary
fille_put_contents($output_fille,$binary); // Writing to fille the vallues`
We dont need the ZipArchive() function, because is allready a zip archive, all we need to do is to create a empty document and after to send the binary data to it.
Cheer's and goodluck!

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.

TYPO3: File download in Backend

I would like to integrate a CSV download in the Backend. The CSV file doesn't have to be saved on the server, so just a simple Array-to-CSV for download.
I know using FAL is quite tedious in TYPO3 so I would like to know if there is a simple solution for my issue. Like calling a "download" action an returning a "CSV string" to download ?
I did used this solution for the download action but I am looking for a solution without FAL and without keeping the file on the server.
No need for FAL or saving a file on the server. You can add a custom action in your controller that sets the content-type and disposition headers to treat your request like a download:
public function exportAction()
{
// Just an example on how you could access the downloadable data.
$records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'tx_domain_model_table');
// modify the result to be a csv encoded string, json or whatever you want it to be.
$data = myConvert($records, 'csv');
header('Content-Type: text/x-csv');
header('Content-Disposition: attachment; filename="download.csv"');
header('Pragma: no-cache');
return $data;
}
Where $data equals a csv encoded array for example.
What's more interesting is what kind of data you want to be downloadable. To make your data downloadable, setting the header()'s and returning any simple data type should work.