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.
Related
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()
I've been working on a in a route of validation for the user in Laravel API to connect to a NLP model in Google Cloud. Making the request go to the API them the API makes a request to a model in Google Cloud. After the prediction is given I pass back to the user.
The problem seems that it takes a certain almost of time to receive a answer from the model so I end up answering a empty JSON to the user.
I wish to ask what is better way to proceed in this situation.
public function request_To_The_Model(Request $request)
{
$data = $request->json()->all();
$response = Http::post(URL, [
$data['sentence']
]);
$body = $response->json();
return response()->json($body);
}
$files = $request->file('images'); // return {}
$_FILES['images']; // return {"name":"sample-passport.jpg","type":"image\/jpeg","tmp_name":"D:\\xampp\\tmp\\php4AD9.tmp","error":0,"size":264295}
Have you tried to remove the Content-Type header? According to this Github issue, it seems to be a problem.
So, I set up a new Laravel installation to test this out and it's working fine on my side. Of course, there's no authorisation whatsoever but this shouldn't impact the result too much.
routes/api.php
Route::post('/profile/upload_image', function (Request $request) {
dd($request->file('image'));
});
Postman configs
Your post input attribute type change file after upload you will get a response.enter image description here
I am learning Laravel 5.4, Still new to Laravel and PHPUnit. Everything is working great after following online basic tutorial.
This test function is working correctly when run phpunit
public function testBasicExample()
{
$response = $this->call( 'GET' , '/welcome');
$this->assertTrue(strpos($response->getContent(), 'Laravel') !== false);
}
Problem comes when I try to test Api
Steps I took
Create Api route for books
Return all users from users talbe as json from localhost/api/books/
public function index()
{
$users = DB::table('users')->get()->toJson();
echo $users;
}
I open the link in browser and json is returned correctly
copy and pasted json into online json validator jsonlint and it is valid.
Create a new test function
public function test_index_method_returns_all_books()
{
$response = $this->call( 'GET' , '/api/books/');
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getContent(),true);
$this->assertJson($data);
}
run phpunit
200 status test passed but assertJson did not pass.
I tried to do var_dump for $response->getContent() and found out it return empty.
now I am not able to get getContent() for api/book/. Does anyone know if there is a solution for this?
Thanks.
Here is a screenshot
Try create some data with a factory before you call the api:
e.g.:
factory(\App\Books::class, 20)->create();
then
$response = $this->call( 'GET' , '/api/books/');
If you had set ":memory:" as your database on phpunit.xml, you will no longer see any data from your local database, that's why you should use factory instead.
How can I register a user from an ionic app via json at my backend.
My backend is build with symfony2 and fos userbundle.
I implemented authentication/authorization via json web token which works seamless.
But I don't know how to register a user and write him through my user entity to the database.
Can anyone give my a approach on hopw to realize this or much better anyone has a little example for this issue.
thanks in advance
bambamboole
edit:
i've created a repository # github:
https://github.com/bambamboole/symfony-jwt
Here a simple example to register a user from an AJAX call using FOSRest and FOSUser bundles :
public function postUserAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$parameters = $request->request->all();
$user = new User();
$user->setUsername($parameters["username"]);
$user->setEmail($parameters["mail"]);
$user->setPlainPassword($parameters["password"]);
$user->setEnabled(false);
$user->setFirstname($parameters["firstname"]);
$user->setLastname($parameters["lastname"]);
$tokenGenerator = $this->get('fos_user.util.token_generator');
$token = $tokenGenerator->generateToken();
$user->setConfirmationToken($token);
$em->persist($user);
$em->flush();
$this->get('fos_user.mailer')->sendConfirmationEmailMessage($user);
$view = View::create()
->setStatusCode(200)
->setData($user);
return $this->get('fos_rest.view_handler')->handle($view);
}
Hope it will help you.