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
#2

Here is a Tutorial that will show you how it is done. If you look in the system folder you will see a RETFUL folder.

Make sure you download the code at the end to view.

Tutorial How to Create RESTful API in CodeIgniter 4 (Complete Guide)
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(05-21-2021, 11:59 AM)InsiteFX Wrote: Here is a Tutorial that will show you how it is done. If you look in the system folder you will see a RETFUL folder.

Make sure you download the code at the end to view.

Tutorial How to Create RESTful API in CodeIgniter 4 (Complete Guide)

That tutorial is very poor.

He's not doing any validation. He's working with JSON and x-www-form-urlencoded. These are easy to parse. What if the user/client sends a multipart/form-data? If the form includes both text inputs and file inputs, one has to use multipart/form-data. The data can't be sent in JSON or x-www-form-urlencoded format. This needs manual parsing, and then, validation.

How does one deal with file uploads in REST? I see no mention of this in the page you linked.

I get an error here when I send text and file (multipart/form-data). Is multipart/form-data illegal? Am I not allowed to use it? How do I upload a file, then?

Currently, it seems to me that request (body) handling is incomplete, and data validation is tightly coupled to JSON/x-www-form-urlencoded, file upload is also tightly coupled to POST method (because of the use of is_uploaded_file() in $file->isValid()). I find these limitations siily.

[Image: Annotation-2021-05-21-232127.png]
Reply
#4

My Point was the System/RESTFUL folder and the RESTFUL trait.

So you see CodeIgniter does handle it, it's up to you to implement it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(05-21-2021, 09:42 AM)akinuri Wrote: 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.

You can. It is implicitly mentioned in the documentation:
Library Reference » Validation
Quote:$validation->run($data, 'signup');


Also it is worth looking at the run() method's phpdoc:
PHP Code:
    /**
     * Runs the validation process, returning true/false determining whether
     * validation was successful or not.
     *
     * @param array  $data    The array of data to validate.
     * @param string $group   The pre-defined group of rules to apply.
     * @param string $dbGroup The database group to use.
     *
     * @return boolean
     */
    
public function run(array $data nullstring $group nullstring $dbGroup null): bool 


As of the rest of your complaints: CI4 is not a complete rewrite of CI3, and as such it has lots of legacy code, so I understand that you find room to improve. With that being said, it is an open source and free software, and everyone is welcome to contribute to it. Better together!
Reply
#6

Restfull  data can be send  json or query paramenter or multipart form


Get delelte requst you could use query patameter with them 

Put post request  json(raw data ) or multipart data(upload files)
Enlightenment  Is  Freedom
Reply
#7

@akinuri Jim (rest in peace) was our REST guru and had big plans for more development when he had to step back during his illness. Since then we haven’t had any experts around, though I’ve tried to keep up with the support classes. The main “missing” component is a REST filter that Jim had planned which would handle (I imagine) most of the parsing and formatting you are asking about. Despite talking with him about it multiple times, this filter still seemed magically abstruse to me, so I have never attempted it.

You seem to have some great REST expertise, and I would welcome your knowledge for improving the framework’s offerings. Would you be willing to parse these roadblocks into actual issues and post them on GitHub for individual discussion and resolution?
Reply
#8

@MGatner I wouldn't go that far and call myself an expert on the subject, but I like analyzing, abstracting, structuring things, so I tend to get concerned with this kind of issues, and can't overlook them.

I was thinking about switching to another framework for the current project (API server), but decided to hack my way (again) to make it work. So, at least for the time being, I will stick to CI4.

I'm almost done writing a system that parses the php://input and populates two arrays, one for the fields (e.g. $_INPUT), and another one for files. I manually populate the $_FILES array, and do error check on the files (for $_FILES['userfile']['error']). I also modified the system files so that I can work with custom fields/files and validate them.

Once it's done, I can describe the issues that I've tried to solve in detail, and share the code.

So, yeah, I can help. But, I'm concerned if I can provide solutions that can be directly injected/applied in/to CI4. I mean, I haven't fully examined the structure of the framework. For example, I'm not familiar with "filters" that you've mentioned. Even though I can provide solutions, someone more familiar with the system might need to implement them.

I had to do some modification related to setting/modifying the environment, and fearing that it won't be a one time thing, I decided to map the flow of the app to figure out the entry points (places I can inject/modify things). I don't know how things are usually progressing, but I find this kind of graphs highly useful. They let me get a perspective on the system in a matter of minutes. I doubt that a newcomer can figure things out by examining the code and then do modifications in just minutes.

This is another issue that I think many libraries/frameworks have trouble with. Documentations are usually meant for the consumers, and not for the contributors/tinkerers. I say this, because I don't think they both have the same concerns. So I find documentations lacking. They tell you about the rules, not how to workaround/improve them (when one needs to).

Anyhow, I'll let you know when I'm done. Other than that, if you have something to add to your suggestion, or have an advice for me about how it's going to work, I'm listening.
Reply
#9

> I'm concerned if I can provide solutions that can be directly injected/applied in/to CI4
That’s where I can help! Jim had very strong opinions about REST, and wanted a “proper” RESTful implementation, as opposed to what he considered “REST-like CRUD” that gets thrown around these days. I’ve pretty much only used REST-like CRUD myself so never felt qualified to take this on. But I am very familiar with the framework and the existing REST classes so if you can get a working implementation then I can work on integrating it.
More on Jim’s work see: https://github.com/codeigniter4/CodeIgni...ssues/2185
Reply
#10

Your app flow is very cool. Would you be willing to share that, if the team thought it was appropriate to include in the docs?

> Documentations are usually meant for the consumers
I agree on this front, and in theory we have resources for framework developers under admin/ and contributing/, but there is still a lot to be done on the framework itself so it hasn’t had the attention it deserves. If you want to share some of your journey of discovery it could be helpful in developing those docs.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB