Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

elle3141's avatar

How to pass an 'api_token' from another server via cURL and receive it in my Laravel project?

I have two servers: shop, which does not use Laravel, and returns which does.

I am trying to send a JSON object from my shop server to my returns server via cURL in the command line, however it is not working.

I previously turned off verification and it was successful, however I need to make it work with verification, in particular by sending a token.

My cURL is as follows:

curl -X POST \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
    --data '{"orderId":"12345", "customerNr":"98765"}' \
    https://returns.ldawson.hygi.test/createReturn

I added the column 'api_token' into the users table and edited RegisterController.php so that an api_key will be created when someone completes the registration form, which works.

I then read the following on https://laravel.com/docs/5.8/api-authentication:

Laravel includes an authentication guard that will automatically validate API tokens on incoming requests. You only need to specify the auth:api middleware on any route that requires a valid access token:

use Illuminate\Http\Request;

Route::middleware('auth:api')->get('/user', function(Request $request) {
    return $request->user();
});

So I added the following to api.php:

Route::middleware('auth:api')->post('/createReturn', 'ProductReturnsController@createReturn');

and removed the following route from web.php, as I guessed that it shouldn't be in both api.php and web.php:

Route::post('/createReturn', 'ProductReturnsController@createReturn');

The method createReturn within ProductReturnsController.php is as follows:

    public function createReturn(Request $request)
    {
        $orderId = $request->input('customerNr');

        $orderId = $request->input('orderId');
    }

Now when I run the above cURL I receive a 404 error and a HTML file:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Not Found</title>

        <!-- Fonts -->
        <link rel="dns-prefetch" href="//fonts.gstatic.com">
        <link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet" type="text/css">

        <!-- Styles -->
        <style>
            html, body {
                background-color: #fff;
                color: #636b6f;
                font-family: 'Nunito', sans-serif;
                font-weight: 100;
                height: 100vh;
                margin: 0;
            }

            .full-height {
                height: 100vh;
            }

            .flex-center {
                align-items: center;
                display: flex;
                justify-content: center;
            }

            .position-ref {
                position: relative;
            }

            .code {
                border-right: 2px solid;
                font-size: 26px;
                padding: 0 15px 0 15px;
                text-align: center;
            }

            .message {
                font-size: 18px;
                text-align: center;
            }
        </style>
    </head>
    <body>
        <div class="flex-center position-ref full-height">
            <div class="code">
                404            </div>

            <div class="message" style="padding: 10px;">
                Not Found            </div>
        </div>
    </body>
</html>

Can anyone please help?

If you need any further information in order to help me, please don't hesitate to ask :).

0 likes
2 replies
elle3141's avatar

@N41TIK - Hey. Thanks for your quick reply. I tried adapting the route, however it still doesn't work, although the html file is now shorter:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="refresh" content="0;url=https://returns.ldawson.hygi.test/login" />

        <title>Redirecting to https://returns.ldawson.hygi.test/login</title>
    </head>
    <body>
        Redirecting to <a href="https://returns.ldawson.hygi.test/login">https://returns.ldawson.hygi.test/login</a>.
    </body>
</html>

It seems to have something to do with the login page. I also have something set up so that users need to be logged in. How can I turn that off?

Please or to participate in this conversation.