简体   繁体   中英

Lumen API + Laravel guzzlehttp response 500 Internal Server Error

I am working on a Lumen + Laravel application wherein Lumen is my RESTful API while my Laravel is the web app itself.

Laravel 5.2.0

Lumen 5.1.6

I have installed guzzlehttp on my Laravel web app for API requests (get, post, delete, put). Whenever I do a GET request, I get a Server error: 'GET http://example.local/api/blog' resulted in a '500 Internal Server Error' response: when retrieving from my Lumen API. But when I do a request thru PostMan, I am able to retrieve data successfully even when I input http://example.local/api/blog directly on the browser, I get the json response.

Lumen GET:

public function getBlogs()
{
    return response()->json($this->sqlConnection()->table(self::TABLE_NAME)->get(), 200);
}

Laravel:

public function index(Client $client)
{
    return $client->get('http://example.local/api/blog')->getBody();
}

I do have other RESTful APIs build with a different framework and tried to test it out if my Laravel web app could connect and it was able to. I find it weird because connecting thru my Lumen API always return a 500 internal server error.

So after hours of debugging, I finally found the answer. On the .env file, there seems to be a conflict with these lines:

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

I removed those on my Laravel web app and I am now able to get a response from my Lumen API!!! (Hooray!) But I wonder, does anybody knows why Laravel is getting those environment variables? I am not encountering any problem when those lines are on while my API is NOT a Lumen framework. It just happens when my API is Lumen. Seems weird to me that Laravel is using those variables.

Also, why do my Lumen API shares or gets value from my Laravel .env file?

Lumen .env:

API_USERNAME=lumentest
API_PASSWORD=lumenpassword

Laravel .env:

API_USERNAME=laraveltest
API_PASSWORD=laravelpassword

Whenever my Laravel makes a request on my Lumen, it gets the API_USERNAME and API_PASSWORD from my laravel app instead on the Lumen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM