0

I have extended WP API plugin to provide an API which can process documents.

Routes

public function register_routes($routes)
{
    $documents_routes = array(
        '/v1/documents' => array(
            array(array($this, 'create_document'), WP_JSON_Server::CREATABLE),
        )
    );
    $new_routes = array_merge($routes, $documents_routes);
    return $new_routes;
}

callback function

public function create_document($data, $_files = null){

    if(!empty($_files)) {
        // process file
    }


    // how to retrieve 
}

Request

POST /api/v1/documents/ HTTP/1.1
Host: localhost
Cache-Control: no-cache
Postman-Token: d5277e1c-12ac-309a-034f-135173b7c7f2
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="doc_id"

3268360
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="PINS REST API Documentation v1.2.doc"
Content-Type: application/msword


----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="case_reference"

EN0102234
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="author"

Ghazanfar
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="stage"

1
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="category"

Application
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="type"

Document
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="description"

This is test document
----WebKitFormBoundary7MA4YWxkTrZu0gW

The Problem

I have some properties such as doc_id, case_reference, description, author, size etc which will be passed to the api along with the file.

Now the problem is I want to be able to retrieve all data if content-type:form-data is used. What is the best way to retrieve all passed data in the callback function efficiently?

0

Found answer to my question.

I didn't want to specify each data parameter in the function definition as it could get long. Therefore, I am retrieving them using $_POST global variable.

I know thats not a good solution, if you know any better let me know.

public function create_document($_files = null){

    if(!empty($_files)) {
        // process file
    }

    $data = $_POST;
    // process $data is you want

    // $data['doc_id']
    // $data['case_reference']
    .....
    .....



}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.