'Get client details in laravel from axios request
I'm building a piece of analytics code in my Laravel 8 project and need to store some data about a client's browser for reporting purposes. I've got several columns in my database and thus build PHP variable containing all of the data.
I'm using methods on the $request object but am finding that this is just picking up the server's details instead of the customer browser's data from the axios request, for example:
$request->path() // this gives me `api/analytics' which is my endpoint, but I'm looking for the webpage URL I'm on, e.g: `window.locatioin.href` for example
Another example would be the ip, I'm using:
$request->ip() // gives me something like 2400:cb00:373:1024::ac46:a2d1 instead of the customer's ipv4 address from their browser.
How would I be able to modify this, I'll attach my front-end axios request and the function that handles that incoming request:
Client side axios request
const res = await app.$axios.post(`${app.$config.apiUrl}/api/analytics`, {
type: 1,
affiliate: 'blah,
campaign: ='blah
}, {
timeout: 30 * 1000
})
Laravel function handling the request
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function __invoke(Request $request)
{
$validator = Validator::make($request->all(), [
'type' => 'required|numeric|in:2,3,4',
'affiliate' => 'required|string',
'campaign' => 'nullable',
'additional' => 'nullable'
]);
if ($validator->fails()) {
return response()->json([
'message' => "One or more fields has been missed or is invalid.",
'errors' => $validator->messages()
], 400);
}
try {
$analytic = [
'user_id' => null,
'brand_id' => null,
'uuid' => null,
'type' => $request->input('type'),
'affiliate' => $request->input('affiliate'),
'brand' => null, // will attempt to set in Microservice
'campaign' => $request->input('campaign') ?? null,
'host' => $request->getHost() ?? 'unknown',
'method' => $request->method() ?? 'unknown',
'path' => $request->path() ?? 'unknown',
'ip' => $request->ip() ?? 'unknown',
'user_agent' => $request->server('HTTP_USER_AGENT') ?? 'unknown',
'fingerprint' => $request->fingerprint() ?? null,
'additional' => $request->input('additional') ?? null,
'session_date' => Carbon::now()
];
// we don't need to instantly process this job, so let's delay it
// for a short period of time to further speed up this function.
$delay = Carbon::now()->addSeconds(15);
AnalyticWatcher::dispatch($analytic)->delay($delay)->onQueue('analytics');
return response()->json([
'message' => "Analytic has been dispatched successfully"
], 201);
} catch (\Exception $e) {
return response()->json([
'message' => $e->getMessage() ?? "Unable to dispatch analytic"
], 400);
}
}
Would $request in the context of sending data from client-side to server-side only be the server ip / page name etc?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
