Luracast / Restler

Simple and effective multi-format Web API Server to host your PHP API as Pragmatic REST and/or RESTful API
http://luracast.com/products/restler/
GNU Lesser General Public License v2.1
1.36k stars 315 forks source link

Using @from header and @from body together #656

Closed kaizirlewagen closed 3 years ago

kaizirlewagen commented 3 years ago

Using version 5.0.5.

I try to move my security token to the header and the "variables" should be in the body. It works if the "variables" are in send in JSON. But if i try to send the data with Parameter content type: text /plain the following error occurs:

{
  "error": {
    "code": 400,
    "message": "Bad Request: `email` is required."
  },
  "debug": {
    "source": "Validator.php:436 at validate stage",

With the following sample i test this:

    /**
     * Test post function for token in header
     *
     * Test post function for token in header
     *
     * @param string $token {@from header} {@required false}
     * @param string $email {@from body}
     *
     * @return array
     *
     * @throws RestException
     */
    function postHeaderTestMethod1(
        $token,
        $email
    ) {
        foreach (getallheaders() as $name => $value) {
            if(strcmp($name, 'token') == 0) {
                $token = $value;
            }       
        }         

        if (is_null($token)) {
            throw new RestException(404, 'Token Not Found');    
        }   

        return 'you have called Api::postMethod1() from ' . $email;
    }   

I checked the Validator.php at line, but i don't find a solution. Any Ideas?

Arul- commented 3 years ago

If email is expected of the body of the request it should be provided in one of the supported formats,

  1. json works as you correctly noted
  2. form request will also work UrlEncodedFormat - application/x-www-form-urlencoded

How to parse email from text/plain ?

kaizirlewagen commented 3 years ago

Sorry for my bad sample... email was only a placeholder.

My goal is to get a raw textarea for text/plain data. But if i try to send raw text to the class function the error above was shown. You can replace email with string.

Is it possible to send text/plain data to the api?

kaizirlewagen commented 3 years ago

I try to get the body data with this:

 // get request data directly from restler
$request_data = $this->restler->getRequestData();

if (is_null($request_data)) {
  throw new RestException(404, 'Could not load any request data.'); 
}

// get data from array
$content = $request_data['request_data'];

I could not fix the issue through validation, so i have to use this workaround.