Welcome Guest, Not a member yet? Register   Sign In
RESTful API Support
#1

I've been using CI3 for a while, and experimenting on CI4 now. I'm planing to use CI4 for an API server. So no front-end. With that in mind, I keep hitting walls. I want to support HTTP verbs (POST, PUT, PATCH, DELETE, etc.) However, I'm getting the impression that CI4 does not (fully) support REST, or haven't been developed with REST in mind.

In PHP, there's no automatic parsing of request body when the request method is PUT or PATCH. So one would have to manually parse the body. I've written several classes that do that. For example, there are several possibilities for the request body (content-type):
  • plain form (application/x-www-form-urlencoded)
  • complex form (multipart/form-data)
  • JSON (application/json)
So far, my code can deal with these all. I can get the data no matter how it is sent. The problem is with the validation. There doesn't seem to be a way/method to set the data manually. There's the withRequest() method, but I don't understand how it's useful. It sets the data directly from the raw input. It's not parsed. How's that going to work? It doesn't. I'd like to be able to set the validation data manually, so I can pass the data that I parsed from the raw input.

PHP Code:
if (in_array($request->getMethod(), ['put''patch''delete'], true))
{
    $this->data $request->getRawInput();


Another problem is with file uploads. When I do a file upload using PUT/PATCH, I manually parse the fields/files, and for convenience, manually populate the $_FILES array. Now, how do I validate the files? The first blow comes from $file->isValid().

PHP Code:
public function isValid(): bool
{
    return is_uploaded_file($this->path) && $this->error === UPLOAD_ERR_OK;


Since the file is not uploaded using the POST method, is_uploaded_file() returns false. Why use this POST-dependent function? Is one not allowed to use anything other than POST?

As a side issue, I also had to disable the fancy/JSON error reporting by commenting out the Services::exceptions()->initialize(); in CodeIgniter.php. When there's an error related to a resource (e.g. a file in form data in binary), it's trying to parse it into a JSON format, and fails.

I'm not sure if I listed all the problems I've encountered, but I fear there'll be more. So what gives? Am I going at this the wrong way? I've decided to use CI4, because I'm familiar with CI3, but was that a mistake? Should be I using another (more REST oriented) framework? I had to hack my way in CI3 many times, and I rather not do the same in CI4. It's tiring.
Reply


Messages In This Thread
RESTful API Support - by akinuri - 05-21-2021, 09:42 AM
RE: RESTful API Support - by InsiteFX - 05-21-2021, 11:59 AM
RE: RESTful API Support - by akinuri - 05-21-2021, 01:37 PM
RE: RESTful API Support - by InsiteFX - 05-21-2021, 08:35 PM
RE: RESTful API Support - by bivanbi - 05-21-2021, 10:19 PM
RE: RESTful API Support - by paliz - 05-22-2021, 03:57 AM
RE: RESTful API Support - by MGatner - 05-22-2021, 05:08 AM
RE: RESTful API Support - by akinuri - 05-22-2021, 07:20 AM
RE: RESTful API Support - by MGatner - 05-23-2021, 04:54 AM
RE: RESTful API Support - by MGatner - 05-23-2021, 04:58 AM
RE: RESTful API Support - by akinuri - 05-25-2021, 12:49 PM



Theme © iAndrew 2016 - Forum software by © MyBB