I need some advice please. I’m creating the following API endpoint and trying to work out how I can read the JSON request body for a PUT or PATCH request. In the ServerConnect > API > Input there are only $_GET and $_POST.
I have a route setup for /api/v1/medical/test with method=PUT
The body contains JSON of { ‘test’: ‘hello’ }, but in ServerConnect I don’t know how to see the JSON body data? See screenshots attached. I have tried $_POST.test and $_GET.test but both don’t work. Request content-type is: application/json, also tried as x-www-form-urlencoded.
Any help would be really appreciated… I’m clearly missing something.
Thanks for posting that, I didn’t know about it. I’m about to start creating our own API so would have definitely stumbled over the OPs issue here, your post has saved me a heap of time working that one out.
I thought I’d give everyone an update and solution here for those that are stuck on this issue.
We ended up adding two lines to our lib/core/app.js file so that it now passes the request body into a $_PUT and $_PATCH variable.You can then use like Apple user suggested earlier, it works great! It would be nice if the guys at Wappler implement it permanently.
To Fix:
Open the /lib/core/app.js file in a text editor (Sublime, Notepad++ or similar)
Create a new line underneath with: $_PUT: req.method == ‘PUT’ ? req.body : {},
Create a new line again, with: $_PATCH: req.method == ‘PATCH’ ? req.body : {},
Save the file.
You can now add JSON into a PUT or PATCH endpoint request body, and get these values by adding the dot notation to $_PUT and $_PATCH. For example: $_PUT.name, $_PUT.test, $_PATCH.test etc.
Obviously if Wappler does an update of the app.js file code you might need to re-implement the change, but it works great for now.
I think it would be easier to just remove the POST method check and use the $_POST always for the request body instead of having different variables for different kinds of requests.
Why can’t we have the standard REST methods available to use as we see fit in our apps? I want to build my apps using standard practices that don’t require sending extra params to define whether something is a delete, an update, or a new object.
That is what Patrick is saying, that you can have all the methods available and no matter of the request method you will have all the data available under the $_POST
Server Connects do not support any methods other than GET AND POST, so API Actions is only alternative. And the API actions still have some defects/limitations that need correcting before being able to support all REST methods.
Thanks for looking at this @George and @patrick. It would be ideal to have all request method data available in $_POST. Make sense to keep it simple. Is it something that could be implemented in an upcoming release please?
I had CORS block any route other than GET and POST initially too. These defaults definitely make sense from a security perspective so no complaints or a bug that I can see. It’s an easy change in the ServerConnect settings under the CORS tab to add the other request methods and origin. Hope that helps.