0

Just recently, support for multipart/form-data upload was added to Restler v3 (source) but I can't get it to work. In my index.php file, I've added:

$r->setSupportedFormats('JsonFormat', 'UploadFormat');

When I post a .txt file, I get the following error (which is expected, since the default 'allowed' format is 'image/jpeg', 'image/png':

"error": {
    "code": 403,
    "message": "Forbidden: File type (text/plain) is not supported."
}

But when I post a .jpg file, I get the following error instead:

"error": {
    "code": 404,
    "message": "Not Found"
}

What am I missing? Here is my function:

function upload() {
    if (empty($request_data)) {
        throw new RestException(412, "requestData is null");
    }
    return array('upload_status'=>'image uploaded successfully');
}
Andrew Bucklin
  • 699
  • 8
  • 19

1 Answers1

1

I figured it out! All I needed a post() function! For anyone who runs into the same issue I ran into, here is my solution for uploading a file with Restler 3:

index.php

<?php
    require_once '../vendor/restler.php';
    use Luracast\Restler\Restler;

    $r = new Restler();    
    $r->addAPIClass('Upload');  
    $r->setSupportedFormats('JsonFormat', 'UploadFormat');

    $r->handle();

Upload.php

<?php
    class Upload {
        function get(){
           if (empty($request_data)) {
              throw new RestException(412, "requestData is null");
           }
        }

        function post($request_data=NULL) {
           return array('upload_status'=>'image uploaded successfully!');
        }
    }
Andrew Bucklin
  • 699
  • 8
  • 19