Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using post data #6

Closed
fi5u opened this issue May 27, 2015 · 8 comments
Closed

Using post data #6

fi5u opened this issue May 27, 2015 · 8 comments

Comments

@fi5u
Copy link

fi5u commented May 27, 2015

Hi, I'm just getting set up with this plugin and I'm wondering how I pass Post data through and then use it from the slim_mapping function.

I'm querying the api with javascript, (specifically Angular):

req = {
    method: 'POST',
    url: CONSTANTS.external.server + '/slim/api/posts',
    headers: {
        'Content-Type': 'application/json'
    },
    data: {
        'abc': '123',
        'def': '456'
    }
}

$http(req).success(function(response, status) {
...

Then in the PHP file, the slim_mapping function looks like:

public function slim_mapping($slim) {
    $context = $this;

    $slim->post('/slim/api/posts', function($data)use($context) {
        $context->addPost($data);
    });
}

private function addPost($data) {
    echo json_encode($data);
}

How do I get hold of the passed data within the data object?

@Botnary
Copy link
Owner

Botnary commented May 27, 2015

req = {
    method: 'POST',
    url: CONSTANTS.external.server + '/slim/api/posts',
    headers: {
        'Content-Type': 'application/json'
    },
    data: JSON.stringify({
        'abc': '123',
        'def': '456'
    })
}

try to stringify your object with json and send it as plain text.
so your request will look like this:

POST /slim/api/posts HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (iPad; CPU OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B554a Safari/9537.53
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Content-Type: application/json; charset=UTF-8
X-Requested-With: XMLHttpRequest
Content-Length: 24
Connection: keep-alive

{"abc": 123, "def": 456}

@fi5u
Copy link
Author

fi5u commented May 27, 2015

Thanks, but it seems like when I try to pass any data, I get a 404 error with: XMLHttpRequest cannot load http://dev.site/slim/api/posts. If I don't pass the data, I don't get the error.
Any ideas what might be causing this issue?

@Botnary
Copy link
Owner

Botnary commented May 27, 2015

In web master tools when you do your request and you don't get 404 what TYPE it says ? POST or GET. Maybe your XMLHttpRequest wrapper when it has an empty body is using GET ?

@fi5u
Copy link
Author

fi5u commented May 28, 2015

It was going through as POST but I found that it didn't seem to like the header content type application/json. So I sent it as text/plain and I no longer get the error. However, I'm also still not able to process the data. Checking the Slim documentation again it seemed the code in $slim->post() was wrong. The following is what I currently have, but it just returns a 500 error.

public function slim_mapping($slim) {
    $context = $this;
    $slim->post('/slim/api/posts', function()use($context) {
        $data = $slim->request->getBody();
        $context->addPost($data);
    });
}

@Botnary
Copy link
Owner

Botnary commented May 28, 2015

and you checked the Apache errors.log and found no clues why you get that 500 response ? and what do you do with data ? do you json_decode it ?

@fi5u
Copy link
Author

fi5u commented May 28, 2015

Nothing is logged to Apache error log when the request is made.
I'm almost certain the problem lies with how I'm getting the data within the PHP code. If I replace the line
$data = $slim->request->getBody(); in slim_mapping()
with
$data = 'abc'
then abc is returned back to the JavaScript calling function with a 200 response. This says to me that the above line is producing an error. Is it correct that it is $slim->request->getBody()?

@Botnary
Copy link
Owner

Botnary commented May 28, 2015

yes you are right, to have access to the $slim object in the callback function you need to add it to the use($context, $slim) and now you have access to $slim object.

public function slim_mapping($slim) {
    $context = $this;
    $slim->post('/slim/api/posts', function()use($context, $slim) {
        $data = $slim->request->getBody();
        $context->addPost($data);
    });
}

@fi5u
Copy link
Author

fi5u commented May 28, 2015

Yes, that works! Many thanks for working through it with me.

@fi5u fi5u closed this as completed May 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants