I am trying this download link in swagger from post call in java using RestAssured. But after hitting the endpoint, the response is some unwanted characters displayed on console. This is a zip file we are talking about. How can i download the file and save it in my local?
Save file from response:
Path pathToFile = new File("path/to/your/file").toPath();
Files.copy(response.asInputStream(), pathToFile);
Related
I have a REST API that is developed using the Java play framework. The API servers the ".zip" file as a response to the request.
Is there any way to test this API using postman? I need to check whether the ZIP file is served properly.
You can use the Save Response button to save the file.
We have some Java services that upload files to an S3 compatible storage and generate Presigned URLs.
Other Java services receive such URLs and work on the files. We need the content length of this file, without loading the whole object from the body. Is this possible? And if yes, how?
We upload via
com.amazonaws.services.s3.AmazonS3.putObject()
and create URLs with
com.amazonaws.services.s3.AmazonS3.generatePresignedUrl()
When I check the Http Headers of a file using my S3 Browser, I can see the correct Content-Length entry.
But in our services we use
OkHttpClient.newCall(
new Builder().get()
.url(url).build()
).execute();
And in those Response objects there is no Content-Length.
If the response is streamed (HTTP/1.1 chunked) or compressed then it won't have a content length to read from. You can probably use a HEAD request to get the headers without the body and check first.
https://stackoverflow.com/a/38673237/1542667
I have updated a method to download the file from the server from XLS to XLSX. I am using apache poi library to generate the excel workbook and then using streaming output entity to send the response back to the client.
Earlier when i was using XLS methods the final response used to look like:
return Response.ok(entity)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=abc.xls")
.type("application/vnd.ms-excel")
.build();
I was getting response as Response.xls, but now when i am using poi-ooxml library with following code, i am getting response without file extension:
return Response.ok(entity)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=abc.xlsx")
.type("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
.build();
Note: I am calling this API from the postman, is the MIME type for xlxs is not correct? File content and everything is correct only the issue is file extension is not appended to file name.
You should also check the http request also.
If possible please provide us the exact request along with valid payload.
The issue was with Postman, postman doesn't work in a way like how browsers work. When same API was called using browser it worked awesomely with no issue. In-spite of using postman for such kind of file download testing better use RESTLET CLIENT. Restlet Client works like a charm for this scenario.
I am trying to figure out how to get the default file name of a file I download using an HttpUrlConnection.
I use a REST API to download the file so I can't parse the URL for the name and the "Content-Disposition" header doesn't contain the name either but when I put the link in my browser it will download the file with the right name so I'm thinking it must be possible to get the name from the result of the HTTP request somehow.
I have read the following two posts which addressed this problem without solving it for my particular situation: HttpURLConnection downloaded file name and
Get download file name from URL or HttpUrlConnection?
I need a download a text/plain file in to a folder. The url does not end with .txt but it has content-type etc... properly set. When I use the browser it immediately prompts me to save the file. The browser automatically puts proper file name also.
Using java how can i download that url in to a folder? Note that I dont know the filename also but I want the file to be saved in a directory.
code to download a file is easy... my question is that I dont know by what name should i save my file. the filename is part of content-disposition header, now how do i extract that?
The HTTP protocol uses the HTTP headers to define some information about the data transferred.
You have the content-disposition header that can have a property filename that is generated by the server. This holds the name of the file being transferred. But it is optional. Should you handle the case it is not present. Here is the doc: http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html
Depending on how you download the file, you'll have dozen of ways to retrieve this file name from the http header.
Give a look to the apache http client for instance.
HIH
M.