Laravel get requests from multipart/form-data in POST method - php

I'm receiving third party request to my laravel post API with two form-data (ID, File) with headers (Content-type multipart/form-data)
In laravel controller, not getting requests in $request->all(), But could see the form-data in file_get_contents('php://input').
In the same request without content-type multipart-formdata, it's working fine, getting requests - id & file. (I've checked in Postman with same third party request params)
Content type as multipart-formdata is requesting from third-party, So couldn't remove or modify the headers. how to properly receive it in laravel using POST method?
Please guide.

Assuming your enctype="multipart/form-data" is correct and file name is myfile,
You should get it as
$file = $request->file('myfile');
in your controller or other class like FormRequest class or Validation class

Related

Why postman form-data return empty request when uploading file to laravel?

Why postman form-data methode return empty request when uploading file to laravel ?
Hello, I trying to send an image to laravel server using postman form-data but the image does not exist in the request and the request is totally empty
this is my controller :
i'm totally blocked here !!
send the request as such :
And it will work
Problem solved, I was using PUT method then i replace it with POST and it did work perfectlly

Set form-data post parameter in Swagger inspector

I have just started swagger for documented api. I am using Swagger inspector for execute the api, before that I was using Postman. I am passing the data in Swagger body but not getting the data, same thing works in Postman but not working here.
Moreover get request works with Swagger but post is not working properly.
Getting response in Postman:
Same thing not working in Swagger:
I have already set the header, but not getting my parameter in post.
That's because you are not setting the Request Headers Content-Type in swagger, as you need to send the POST request you need to send the data via the Body tab and to send the data you need to specify the content type for the request body so do the following
Add a Content-Type : application/json header from the Authentication&Headers tab like below
if you are using any authentication headers for the request when using postman add them too
Now go to the Body tab
and add the following
{
"userName": "123",
}
That's all you need to do now click send and it will show you the data sent, just select any format for the request Headers and then provide the data in that format. Below are the supported formats

Could not submit form-data through postman put request

I m using lumen api 5.6.
This is my route
$router->PUT('collections/{collectionId}'
In postman i m calling this api like this.
and this is the body of that request
See here i have CollectioPoints here but its failing in validation.
the data i m sending through formdata is not able to recognized in validation.
But if i send the route from put to post [$router->POST('collections/{collectionId}'], all the data i m sending through form data are recognized and i m getting correct response as below
Why this is happening. Cant i send form data through put request?
Thaank you.
Laravel(Lumen) cheats because html forms only support GET and POST, but it does understand a real PUT/PATCH request.
In Postman You should send POST and set _method to PUT (same as sending forms) to make your data and files visible
_method = "PUT"
Or
If you don't have any file only have data then you can write you parameter in choose below option in Postman
x-www-form-urlencode
Or
You want use PUT method for send Data with form-Data option you should use stuff like this https://gist.github.com/devmycloud/df28012101fbc55d8de1737762b70348
Example
Headers: Content-Type application/x-www-form-urlencoded
_method = "PUT"
an your controller
return $request->file('avatar');
image example

RESTful API PHP Content Type

I am writing a RESTful API in the CakePHP framework. Using the POSTMAN app I am testing a delete method with the headers set as follows:
Content-Type application/json
I am sending my data in the body as RAW:
{"id":"8"}
MY API is routing the request as I would expect to my controller, and I'm reading the body with:
json_decode(file_get_contents('php://input'),'true');
This gives me empty data with the above parameters. However if I change the method type to POST or I remove the content type form the headers then I get content in the body.
Any ideas, seems like strange behaviour!

Unable to POST data from mobile App to Laravel 4 Application

I have an android application which sends some data in a POST request to my web application built with Laravel 4. I tried testing the web application by sending a crafted POST request from the REST Console chrome extension. I have used Resource routing in my application, and so in the "store" method of the controller, I tried to get the POST data using Input::get("key"). This however gives me "null" . Even Input::all() returns null.
Can someone help me figure out how I can achieve this. I cannot use Model binding directly, as I need to perform some validation on the data before storing it.
$request = Request::instance();
$content = $request->getContent();
dd($content);
This snippet printed out the data that has been passed out via POST viz. string(25) "test=12121&variable=12123".
Weirdly enuff dd($_POST) gave array(0) {}

Categories