I am trying to test an API to accept multipart form-data. I am trying to use the PhpStorm rest client. I set the method to Post and the path to my endpoint and then under request body I checked Text, put some text in there and I also check File Upload(multipart/form-data) and put the path and filename into the File To Send text box.
When I click debug and run the request, I only receive the text. I do not get a file upload. If I uncheck text and leave File upload clicked, it properly sends the file.
I need two things. I need to be able to send text in the message body and I also need to be able to attach multiple files. In other words, I need to set up a true multipart/form-data post request.
Any suggestions would be greatly appreciated.
That is not currently possible with built-in REST Client [1 & 2].
https://youtrack.jetbrains.com/issue/WI-40562 -- watch this ticket (star/vote/comment) to get notified on any progress.
Please note that RESTful client is now considered obsolete(?) as JetBrains have introduced Editor-based Rest Client which will replace the GUI tool and will be the one that will get future development/new features. See more here:
OK - I figured out one way to do it. Thanks for pointing me down the right path!
I am not sure why JetBrains is dumping the gui REST client. It certainly had more potential than creating the rest calls manually through the new edit method, but that is something I will bring up with them.
Step 1: edit the php.ini file on the webserver (in my case a vagrant box) and set:
xdebug.remote_autostart=1
Save the file and reboot your webserver and php-fpm (or fastcgi etc...) if you are using one of those services.
At this point, we no longer need to worry about triggering xdebug which, although it is possible to do within your rest call**, it continually gave me problems.
Step 2:
Manually create your call and make the Content-Type: multipart/form-data. Here is an example (please note, line feed (returns) are important!):
POST http://hostname/endpoint/
Accept: */*
Cache-Control: no-cache
Content-Type: multipart/form-data; boundary="abcd"
--abcd
Content-Disposition: form-data; name="json_string"
{"id”:”1234”,”message”:”here are your files”}
--abcd
Content-Disposition: form-data; name="file1"; filename="test.pdf"
< /pathToFile/test.pdf
--abcd
Content-Disposition: form-data; name="file2"; filename="secondfile.txt"
< /pathToFile/secondfile.txt
--abcd--
Step 3: Place your breakpoints in your code and fire the http call from the little green arrow on the left side by line number 1.
Your breakpoint should now be triggered and you can examine the results of your call.
** NOTE: If you do want to attempt to have the api call fire xdebug, you can do it by tacking on a session start to your endpoint like so:
POST http://host/endpoint/?XDEBUG_SESSION_START=PHPSTORM
Accept: */*
Cache-Control: no-cache
XDEBUG_SESSION=PHPSTORM
Content-Type: multipart/form-data; boundary="abcd"
However, I ran into multiple problems with this method. I am sure something like it could work, but I couldn't figure out how to do it. ;-)