Laravel Api Postman Upload Image Return Null - laravel

$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

Related

Laravel HTTP Client does not work with empty body but Postman works

So I'm having an interesting issue with Laravel HTTP Client while trying to hit an API endpoint for PayPal.
I can get Laravel HTTP Client working on all my endpoints that POST with data, but this one endpoint that only requires headers (no data is passed in the body) fails with an error.
{
"name":"INVALID_REQUEST",
"message":"Request is not well-formed, syntactically incorrect, or violates schema.",
"debug_id":"609388c4ddfe4",
"details":[
{
"field":"\/",
"location":"body",
"issue":"INVALID_SYNTAX",
"description":"MALFORMED_REQUEST_JSON"
}
],
"links":[
{
"href":"https:\/\/developer.paypal.com\/docs\/api\/orders\/v2\/#error-INVALID_SYNTAX",
"rel":"information_link",
"encType":"application\/json"
}
]
}
When I hit the same endpoint in Postman everything works fine
My method for hitting the endpoint looks like this
public static function capture($order)
{
$token = Paypal::fetchToken();
$api_url = config('services.paypal.api_url') . '/v2/checkout/orders/' . $order['id'] . '/capture';
$headers = [
'Content/Type' => 'application/json',
];
$response = Http::withToken($token)
->withHeaders($headers)
->post($api_url)
->json();
return $response;
}
I have tried passing an empty array in the post request like this ->post($api_url, []) but that did not work either.
I have hardcoded the $api_url just in case I made a mistake with my formatting with variables. Resulted in the same issue.
I have tried changing the 'Content/Type' in the header to 'none'. This did not work either and also doesn't make sense because I have this same header set in postman and it works fine (PayPal docs also says to pass this content/type)
Based on the error I am receiving I can only assume the request is hitting the endpoint correctly, but either the HTTP wrapper or guzzle itself is adding something to the body when I leave it blank and it is causing PayPal to throw the error. Don't really know what else I can try though.
Is there a parameter I am overlooking for specifying an empty body on a post request?
Any help is appreciated.
Looking at the source I found the following solution
$response = Http::withToken($token)
->withHeaders($headers)
->send("POST", $api_url)
->json();
I had the same issue, but I fixed it with a simple trick.
I found the solution on https://docs.guzzlephp.org/en/stable/request-options.html#json.
This code should work.
$response = Http::withToken($token)
->withHeaders($headers)
->post($api_url,['json' => []])
->json();
The empty array is now seen as an empty array/body in JSON.

Laravel Api returns Html instead of json

Hello there my website is on cpanel and when i make an api call it returns contnet-type html instead of json note that it worked perfectly on my localhost but for some reason it isn't working
Code
public function fetch_companies()
{
//getting company that is linked to coupon
$companies_id = Coupons::where('company_id' , '!=', 'null')->pluck('company_id');
$companies = Companies::whereIn('id' , $companies_id)->get();
return response()->json($companies);
}
i tried setting the headers to json like this
return response()->json($companies)->withHeaders([
'Content-Type' => 'application/json',
]);
but it didn't work
here is the link to my website you may test it using postman
http://coupon-app.epizy.com/company/api/fetch
just to put you in the picture the code currently running this page is this
public function fetch_companies()
{
//getting company that is linked to coupon
$companies_id = Coupons::where('company_id' , '!=', 'null')->pluck('company_id');
$companies = Companies::whereIn('id' , $companies_id)->get();
return response()->json($companies)->withHeaders([
'Content-Type' => 'application/json',
]);
}
if you need any information please comment and Thanks in Advance
well i found out that the cause of this problem was my server provider as you can find here
https://infinityfree.net/support/javascript-error-using-api-or-mobile-android-app/
it is a security "feature" although it is not a feature that blocks any request that doesn't accept cookies and run javascript
Change Accept & Content-Type to Application/json on the header

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.

laravel api with vue 2 js not returning data - could 'localhost:8000' (or '127.0.0.1:8000') be the issue?

I am using the repo https://github.com/mschwarzmueller/laravel-ng2-vue/tree/03-vue-frontend so I have 100% confidence in the reliability of the code. I can post through the laravel api endpoint through the very simple Vue client, and also through Postman. Through Postman I can retrieve the table data array, but not so in the client app. In POSTMAN:
localhost:8000/api/quotes
works just fine.
IN THE vue 2 js CLIENT APP:
methods: {
onGetQuotes() {
axios.get('http://localhost:8000/api/quotes')
.then(
response => {
this.quotes = (response.data.quotes);
}
)
.catch(
error => console.log(error)
);
}
returns nothing. returning the response to Console.log returns nothing. The Network/XHR tab shows the table data rows, but I am not sure what that means.
I know for sure that this code works for others with their unique api endpoints, which I assume may not use localhost or '127:0.0.1:1080.
Edit: in response to request for more info
public function getQuotes()
{
$quotes = Quote::all();
$response = [$quotes];
return response()->json($response, 200);
}
and the relevant route:
Route::get('/quotes', [
'uses' => 'QuoteController#getQuotes'
]);
Just to confirm: I am using verified github repo code in which the ONLY change is my api endpoint addressas mentioned in the first line of the body of this question. . Note that the Laravel back end is also derived from a related repo in Max's fine tutorial. The running code can be seen at
So I really don't think this is a coding error- but is it a configuration error due to me using local host??
EDIT: It WAS a coding error in the laravel controller as shown below
The reason your code isn't working if because you haven't provided a key for your $quotes in your controller but you're looking for it in your vue file (response.data.quotes).
[$quotes] is essentially [0 => $quotes] so when the json response comes through it be 0: [...] not quotes: [...].
To get this to work you just need to change:
$response = [$quotes];
to:
$response = ['quotes' => $quotes];
Furthermore, just an FYI, you don't need to provide the 200 in response->json() as it's the default and you can just return an array and Laravel automatically return the correct json response e.g.:
public function getQuotes()
{
$quotes = \App\Models\Artist::all();
return compact('quotes'); //<-- This is just another way of writting ['quotes' => $quotes]
}
Obviously, you don't have to if you don't want to.
Hope this helps!

return file response in laravel not working

I'm using laravel 5.2 , I've used response()->file() function to return file. On localhost it is working as expected but on live server file is being downloaded automatically (with no extension). But i wish to open it instead of downlod. Anyone can help?
Here is my code:
public function returnFile($slug)
{$file = Mixes::where('id_name,'=',$slug)->get()->first();
return response()->file('./path/to/file/'.$file->name);}
Thanks.
You'll need to add header to your response.
Response with header example:
$response->header('Content-Type', 'application/pdf');
Simple example:
$file = File::get($file);
$response = Response::make($file, 200);
$response->header('Content-Type', 'application/pdf');
return $response;
Then the file will display in your browser window.
This solution will work with files pdf,docx,doc,xls.
Hope it will help!

Resources