2

I have an api to export some information to a csv file. The API is correct and it is downloading my file when I access it from the browser. I need to access this API from the terminal and download the file without having to go to the browser.

My route for the API looks like this:

Route::get('/api/file/export', 'File\FileController@export', [
    'middleware'=>'auth.basic'
]);

I tried using curl like this:

curl --user email:password http://example.com/api/file/export

I have tried different curl commands but each of then displays the redirect to login html. When I use -O the command for downloading a file, it downloads a file that has the redirect to login link.

curl --user email:password -O http://example.com/api/file/export

Am I calling the API correctly? How else can I access the API from the terminal?

Phillis Peters
  • 2,232
  • 3
  • 19
  • 40
  • It looks like your using basic HTTP authentication, but what you describe as the error seems like a typical username/password auth. Have you modified the `auth.basic` middleware from what is default in Laravel? – The Maniac Sep 28 '16 at 16:07
  • No I have not modified the `auth.basic` file. What changes should I make? – Phillis Peters Sep 29 '16 at 07:07
  • Hmm. Maybe I'm mistaken but you shouldn't see "login html" if you are using HTTP auth. It should just be a 401 Unauthorized response, thats it. Regardless, usually you would use token-based authentication when you are building an API that needs security. [Fortunately Laravel has that sort of thing baked in](https://laravel.com/docs/5.3/passport) (although I know it requires you change gears). – The Maniac Sep 29 '16 at 20:03
  • I was getting 302 redirect as the HTTP response, but I actually decided to switch to token-based authentication yesterday. I am adding the API to an old project that uses Laravel 5.1 so I can't use Laravel Passport instead am using this [plugin](https://github.com/lucadegasperi/oauth2-server-laravel). Then I will use a Python script to call the API from the terminal. – Phillis Peters Sep 30 '16 at 06:44

1 Answers1

3

You should first be logged in your website. You can try this:
curl --user email:password http://domain.tld/login_page

And then use the cookies for your second request:
curl --cookie http://domain.tld/file/to/export

If that is not working, you need to do the whole submit action with cURL, meaning doing POST request with email and password etc.

Someone gave a good solution here

PS: Checkout if you don't need a token to request your API too.

Community
  • 1
  • 1
Amitsouko
  • 175
  • 5