Could not submit form-data through postman put request - php

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

Related

Sending multipart/form-data with PUT request doesn't work in Laravel

I am trying to send an HTTP PUT request with "Content-Type": "multipart/form-data" to a Laravel application. When I change the method to POST it works.
$a = $request->all(); // With PUT this is empty but with POST it works fine.
The client-side executes the following code:
axios({
method: "post", // when I try method:"PUT" and change the content type
url: "/api/offer",
data: fd,
headers: {"Content-Type": "multipart/form-data"} // here change to "x-www-form-urlencoded" it the $a array on backend is empty!
}).then(response => {
console.log("/offer/" + response.data)
if (response.data)
window.location.replace("/offer/" + this.offer.id);
else {
console.log("show a message that something went wrong! ")
}
}).catch(function (error) {
})
I could not find anywhere in the docs that PUT can't send "multipart/form-data"
So, can PUT send "multipart/form-data" or only POST can do that in general or it is only a PHP / Laravel Issue?
Edit:
Also, what difference does it make to use PUT instead of POST other than to comply with HTTP protocol and CRUD operation properly?
Laravel (HTML Forms) do not work great with Put requests so you'll need to spoof a POST request as if it was a PUT or PATCH request. On Axios you use the .post verb but within your form data you append
_method: "put"
Information from the official documentation:
https://laravel.com/docs/8.x/routing#form-method-spoofing
Excerpt from the documentation:
HTML forms do not support PUT, PATCH, or DELETE actions. So, when defining PUT, PATCH, or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method
I ran into this issue a few weeks ago myself with a Symfony 5.3 project. It only worked with POST Requests, not with PUT. Here's an issue from the Symfony GitHub that explains it in more detail.
To my understanding the issues lies within the PHP implementation of those requests. The HTTP standard "PUT" supports it, but PHP does not. Here's also a link to the bug from the PHP bugtracker.

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

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

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

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!

POST data not received on server side

I use Postman (the Chrome app) to send POST data to a URL but the POST data are never received by the PHP file, no matter how I change the content-type before sending. Is there a server setting on Apache that stops post data from external sources?
This is the URL:
http://friendesque.com/arranged/handler.php
And this is the content of the handler.php file:
<?php
echo("Inside file");
echo("JSON:");
$json = file_get_contents('php://input');
echo($json);
echo("POST:");
print_r($_POST);
echo("GET:");
print_r($_GET);
?>
In order for data to be available in $_POST array, the following conditions should be met:
Request must be sent with POST HTTP method
Content-Type header must be set to application/x-www-form-urlencoded
Request payload (body) must be in the form of URL-encoded parameters, e.g.
param1=a&param2=b
I sent to your URL a request that meets those 3 conditions and got my data available in $_POST array.
Use file name like /index.php . It will work !!

Categories