Laravel API Request - Not able to display Posted Data/Passed Parameters - laravel

I need help on my Laravel API,
I have an external API Request written in C#
Method: Post
Data is JSON which is being posted to my Laravel API URL: http://localhost/project/api/company-data
API route:
Route::post('company-data', 'API\APIController#index');
Here is my API Controllers index() function
public function index(Request $request)
{
return var_dump($request->all());
}
My API Call is hitting the function since I was able to capture it on logs,
However it is returning me an empty array?
How do I catch the json data that was posted to my API?
Thank You for your help, my apologies if I cannot explain my concern clearly,

If found the solution,
It is $Request should be
json_decode($request->getContent(), true);
instead of
$request->all()

Related

How to call resource route Codeigniter 4

Please I need help to enable me know how to map the resource route to http request.
for example am trying to call this url via axios
http://localhost/nmc/public/api/unregisteredpatients?userid=hen11#gmail.com
and i have created the following resource routes to call my api
//$routes->resource('api/unregisteredpatients/(:any)', 'UnregisteredPatients::show/$1', ['namespace' => 'Api']);
//$routes->get('api/unregisteredpatients/(:any)', 'UnregisteredPatients::show/$1', ['namespace' => 'Api']);
$routes->resource('api/unregisteredpatients/(:any)', ['controller' => 'Api/UnregisteredPatients']);
None of these was able to call my api method instead they all called the index() method, whereas I wanted it to call the show($id) method to enable me utilise the $id to fetch data.
public function show($id = null) {
.....
}
Please I need help
This solves it for me, though I couldn't have a way to capture query parameters but passing the parameter as a segment gives me an AFAIK solution.
$routes->match(['get'], 'api/unregisteredpatients/(:any)', 'Api\UnregisteredPatients::show/$1');

API endpoint in Laravel hook

Is it possible to establish an API endpoint in a laravel voyager hook?
All the documentation I can find shows how to set up a listener for a web request, but these endpoints will not take a post.
Just create the post route in your api routes file.
Example:
// routes/api.php
<?php
Route::post('/webhooks', 'WebhooksController');
// app/Http/Controllers/Api/WebhooksController
public function __invoke(Request $request)
{
// handle webhook
}
Then posting data to yourapp.test/api/webhooks should work.

What is better practice for accessing laravel request variables

What is better practice for accessing request values in laravel.
For example I have method update() in laravel controller.
I want to get values from request, which is better way:
method(Request $request) {
$request->inputName;
}
or
method() {
request('inputName');
}
Is it better to create request instance as method attribute or to use Laravel helper method request().
Thanks
Both are the same, the first approach might be better in case you create a custom form request where you do the validation of the form. Other than that both provide the same thing.
In your Controller
use Illuminate\Http\Request;
public function update(Request $request)
{
$name = $request->input('name');
}
I think this is best way to use
method(Request $request) {
$request->inputName;
}
Even laravel documentation suggest this.
laravel provides you with methods to get values from the request
now I have request variable is $request, it is instance of Request
The better practice for accessing laravel request variables:
If GET method, you should use $request->get('variable_name')
If POST method, you should use $request->input('variable_name')
if you want check request has variable you can use $request->has('variable_name')
Good luck

Laravel 5.5 Request is empty in Restful controller

I have such a route in my routes/web.php
Route::resource('/api/surveys', 'SurveyController');
As documentation says, it creates all needed routes for API. This is a function, that gets executed when I go for /api/surveys route:
public function index()
{
$request = request();
if(!$request->hasHeader('token')) {
return "No auth token found.";
}
$tokenCheck = $this->userService->isTokenValid($request->header('token'));
if($tokenCheck !== true) {
return $tokenCheck;
}
return $this->surveyService->all();
}
What it does, it checks if token header parameter is set, if not, it returns an error, if yes, it checks if token is valid and etc. if everything is OK, it should return surveys from database.
public function surveys() {
$request = \Request::create('/api/surveys', 'GET');
$request->headers->set('Accept', 'application/json');
$request->headers->set('token', \Cookie::get('token'));
$response = \Route::dispatch($request);
print_r('<pre>');
print_r($response);
print_r('</pre>');
}
I have a website, that should use that API I just created to get all survey records. I create a new request object, set header "token" with token I get from a cookie and then try to dispatch and get a response. But the problem is that everytime I get "No auth token found." error. That means $request->hasHeader('token') returns false, even tough I set it here in my request. If I print_r $request->all() in Restful controller, I get an empty array.
I tried Postman to access this API with token parameter, and it works fine in postman, but here, it seems that Request disappears while it travels to API controller.
What I did wrong here?
When you manually create a request and dispatch it, that works to get the routing to call the correct controller, however that does not affect the request that is bound in the container.
When your "fake" request is handled by the api controller, the request that it pulls out of the container is the original "real" request that was made by the user.
Instead of dispatching the route with your new request, you will need to app()->handle($request) the new request. This, however, will completely replace the original "real" request with your new "fake" request, so everything from the original request will be lost.
Having said all that, this method of consuming your own api is discouraged, even by Taylor. You can read his comment on this Github issue. So, consuming your own api like this may work, but you may also run into some other unforeseen issues.
The more appropriate solution would be to extract out the logic called by the api routes to another class, and then call that extracted logic from both your api routes and your web routes.

To create restful api for android team in Laravel 5.2

I am currently creating Rest API for Android Team using Laravel 5.2 and testing the API in RESTCLIENT.
I am just trying to get the form values which are entered in Restclient with POST method.
I have problem with POST method.. I m just not able to get the form field values. My API for GET method works fine.
Kindly guide me.
Thanks
This is how you handle POST request in laravel. Be it 5.2 or 5.4.
Route::post('books', 'BookController#store');
Controller
pubic function store(Request $request)
{
$name = $request->name;
$name = $request->get('name');
$name = request('name'); //global helper
//Your testing with api so you should return to api if you want to see it
return response()->json($name);
}
Then check in Android console if you receive $name in JSON format.

Resources