Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
REST Controller POST JSON
  • Hi there, I am in the process of building an ExtJS application that is backed by FuelPHP. I have been pulling my hair out trying to get the following to work: I have a REST controller that has a POST action assigned to it. Because of the architecture of ExtJS I need to POST JSON data to this action. However the Input::.Post() array is completely empty when I do this. Is this possible with Fuel? Many thanks for your help spreeech
  • I am having the same problem, i posted data through Ajax going to Rest Controller but when it arrives in Rest Controller, input::post does return null. have anyone solved this problem? thanks
  • Hi merdj, I manged to get it working using:
    file_get_contents('php://input');
    

    (the #40 and #41 are open and close brackets) That should return the raw http post data. You will then need to deserialize the JSON back out into a PHP object array.
  • Hello Spreech, can you show me please how you do it from JS to Controller Rest, can't figure out how to apply your solution. thanks a lot note: If I use Get instead of Post everything works fine, it returns the result I need. But, what I need is Post and not Get. with this I need your help
  • Try: $params = Input::post(null); echo $params;
    echo $params;
    or directly: $url = Input::post('url');
    $title = Input::post('title');
  • Any news from you Spreech? thanks
  • Hi Merdj, I have now blogged about this to give you a better explanation of my solution. Check it out here: http://samfoot.co.uk/?p=45 Cheers
  • Hi Spreech, Wonderful! it works perfectly,, God bless you and thanks a lot.
  • Hi Merdj, Great to hear that my solution worked for you! Sorry about the delay in getting back to you. Thanks for reminding me! In your comment you ask about validation, what sort of validation are you after? Validation of the posted JSON for length, data type, regex etc? Let me know and ill see if I can get some code together! Cheers
  • hello Spreech, im sorry for late reply, i am too busy for the past days. i have the following code: class Controller_Checker extends Controller_Rest { public function post_contact_us()
    { if (input::method() == 'POST')
    {
    $item = Format::factory(file_get_contents('php://input'),'json')->to_array(); $val = Validation::factory('contact_us');
    $val->add_field('name', 'Your Name', 'required|min_length[3]|max_length[50]');
    $val->add_field('email', 'Your Email', 'required|valid_email');
    $val->add_field('subject', 'Your subject', 'required|min_length[3]');
    $val->add_field('message', 'Your message', 'required|min_length[4]'); if ($val->run())
    {
    $this->response(array('valid' => true, 'message'=>$item, 'redirect' => '/'), 200);
    }
    else
    {
    $this->response(array('valid' => false, 'message'=>'validation error', 'redirect' => '/'), 200);
    }
    }
    }
    } the above code does not work, i am not very sure but the validation seems does not work. any help could is heartily appreciated. Best regards,
  • is there anything in print_r($_POST)
  • Hi bperin, It seems as though $_POST is completely empty: If I post this:
    {"_id":"","url":"http://asda.com","title":"asdasd";}
    
    And then return this:
    public function post_add()
    {
            $this->response(print_r($_POST));
    }
    
    All I get in my response is:
    Array
    (
    )
    true
    
    Many thanks spreech
  • If your using Firefox, open up firebug and go to "Net" tab.
    See how the request is sent and if all your data is actually sent to the right place.
  • Ran Yefet wrote on Saturday 1st of October 2011:
    If your using Firefox, open up firebug and go to "Net" tab.
    See how the request is sent and if all your data is actually sent to the right place.

    The data that I put in my second post is lifted straight from firebug. It doesn't seem that there is a way to access the raw HTTP POST from a rest controller.
  • Sorry for the double post but I have found a temp solution: Using
    file_get_contents('php://input');
    
    returns the posted JSON correctly. There may be a better way to achieve this without having to use the above but it has given me a way to get my app working in the meantime. cheers for your help guys. spreech

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion