368

I have an API endpoint and an Authorization token for that API.

The said API is for .xls report download. How can I view the downloaded .xls file using (if possible) Postman?

If it is not possible using Postman what are the other programmatic ways I should be looking for?

2
  • 4
    @nbokmans I want to download .xls file provided by backend when i use postman, i am not able to view .xls file properly, its all unicodes and special characters. What I need is, If there is any other programmatic way apart from postman for firing an api and downloading .xls file on my pc Commented Aug 16, 2016 at 15:03
  • @nbokmans Thanks for reply, it just starts download, it doesn't give any location. Commented Aug 16, 2016 at 15:50

7 Answers 7

882

Try selecting send and download instead of send when you make the request. (the blue button)

Screenshot showing "Send and Download" button in postman

https://www.getpostman.com/docs/responses

"For binary response types, you should select Send and download which will let you save the response to your hard disk. You can then view it using the appropriate viewer."

Sign up to request clarification or add additional context in comments.

5 Comments

this is definitely not intuitive - thanks for the pointer! Tried Save Response but it just saved to Examples which wasn't helpful.
This only downloads the response.json and not the file itself.
I tried this, and it downloads the raw base64 encoded file as a text file, not as the file itself...
I've never thought to check the combobox! Damn!
you saved my life!
31

You can Just save the response(pdf,doc etc..) by option on the right side of the response in postman check this image postman save response

For more Details check this

https://learning.getpostman.com/docs/postman/sending_api_requests/responses/

2 Comments

It seems to ok. But there is not Save Response option for GET or POST requests. Should I use Send and Download option on Postman?
for each request, you will get an option to save the response, but as you mentioned you can use the send & save option as well
9

If the endpoint really is a direct link to the .xls file, you can try the following code to handle downloading:

public static boolean download(final File output, final String source) {
    try {
        if (!output.createNewFile()) {
            throw new RuntimeException("Could not create new file!");
        }
        URL url = new URL(source);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        // Comment in the code in the following line in case the endpoint redirects instead of it being a direct link
        // connection.setInstanceFollowRedirects(true);
        connection.setRequestProperty("AUTH-KEY-PROPERTY-NAME", "yourAuthKey");
        final ReadableByteChannel rbc = Channels.newChannel(connection.getInputStream());
        final FileOutputStream fos = new FileOutputStream(output);
        fos.getChannel().transferFrom(rbc, 0, 1 << 24);
        fos.close();
        return true;
    } catch (final Exception e) {
        e.printStackTrace();
    }
    return false;
}

All you should need to do is set the proper name for the auth token and fill it in.

Example usage:

download(new File("C:\\output.xls"), "http://www.website.com/endpoint");

Comments

9

For UI, choose Send and Download button instead of Send button in Postman: enter image description here

Comments

8

In postman - Have you tried adding the header element 'Accept' as 'application/vnd.ms-excel'

Comments

8

In the response section follow these steps:

enter image description here

1 Comment

this just works fine.
1

My answer is for those that are trying to download excel file in laravel using api route. It's important to note that I was using laravel Jobs and dispatch stuff. And I was sending the return request to a json_success helper function which was actually converting my download response to a json. So I simply removed anything that comes in between downloading file. And it's working fine now. As I am using repository structure so I will try to post each function from repository to controller. Dispatch was also giving me 0 which wasn't letting the file to be downloaded.

Before

// export service data // Repository function
public function exportInsuranceServices($insuranceId)
{
    return dispatch_sync(new ExportInsuranceServiceJob($insuranceId));
}

this is the job class

class ExportInsuranceServiceJob implements ShouldQueue
{
use Dispatchable, Queueable;

protected $insuranceId;

public function __construct($insuranceId) {
    $this->insuranceId = $insuranceId;
}

public function handle()
{
    return Excel::download(new ExportInsuranceService($this->insuranceId), 'insuranceService.xlsx',null, [
        'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    ])->getStatusCode();
}
}

Export Insurance Service

class ExportInsuranceService implements FromQuery, WithHeadings, ShouldAutoSize
{
use Exportable;

protected $insuranceId;

public function __construct($insuranceId) {
    $this->insuranceId = $insuranceId;
}

public function query()
{
    return InsuranceService::query()
        ->where('insurance_id',decryptId($this->insuranceId))
        ->select('name_en', 'name_ar','code', 'price','original_price', 'insurance_id','service_id','status');
}

public function headings(): array
{
    return [
        'name_en', 'name_ar','code', 'price','original_price','insurance_id', 'service_id','status'
    ];
}
}

Controller function

/**
 * Export Insurance Services by insurance id.
 */
public function export($insurance_id,InsuranceServiceRepository $repo)
{
    return jsend_success($repo->exportInsuranceServices($insurance_id));
}

After

// export service data. Here I have removed the job class
public function exportInsuranceServices($insuranceId)
{
    return Excel::download(new ExportInsuranceService($insuranceId), 'insuranceService.xlsx');
}

/**
 * Export Insurance Services by insurance id. Here I have removed jsend_success helper function
 */
public function export($insurance_id,InsuranceServiceRepository $repo)
{
    return $repo->exportInsuranceServices($insurance_id);
}

The ExportInsuranceService is the same as already shown

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.