I have an android application which sends some data in a POST request to my web application built with Laravel 4. I tried testing the web application by sending a crafted POST request from the REST Console chrome extension. I have used Resource routing in my application, and so in the "store" method of the controller, I tried to get the POST data using Input::get("key"). This however gives me "null" . Even Input::all() returns null.
Can someone help me figure out how I can achieve this. I cannot use Model binding directly, as I need to perform some validation on the data before storing it.
$request = Request::instance();
$content = $request->getContent();
dd($content);
This snippet printed out the data that has been passed out via POST viz. string(25) "test=12121&variable=12123".
Weirdly enuff dd($_POST) gave array(0) {}
Related
I'm receiving third party request to my laravel post API with two form-data (ID, File) with headers (Content-type multipart/form-data)
In laravel controller, not getting requests in $request->all(), But could see the form-data in file_get_contents('php://input').
In the same request without content-type multipart-formdata, it's working fine, getting requests - id & file. (I've checked in Postman with same third party request params)
Content type as multipart-formdata is requesting from third-party, So couldn't remove or modify the headers. how to properly receive it in laravel using POST method?
Please guide.
Assuming your enctype="multipart/form-data" is correct and file name is myfile,
You should get it as
$file = $request->file('myfile');
in your controller or other class like FormRequest class or Validation class
I'm maintaining a backend API written in slim 2. the api is mostly used by mobile apps, so I'm looking to switch from sending requests as POST data to sending JSON formatted strings. This will simplify the api, as the apps currently sending arrays, and even json strings as post data variables.
While it's simple to release an update to the mobile apps and switch to json formatted POST requests, the backend api will have to keep supporting both formats for some time until all users will update their apps.
I'm looking to have a replacement function to $app->request->post() that will either get the request param from the post data or from the json data, depending on the content type that was sent by the client.
For example, im using $app->request->post('user_id', 0); to get the variable from the post data.
I got installed into my api and it does convert the json body into an array. the problem is that now each of my api functions have to check either $app->json_body array or the $app->request->post function to retrieve the request params.
For example:
$app->get('/settings', function () use($app, $freeze)
{
if ($app->json_body != null) {
$user_id = $app->json_body['user_id'];
} else {
$user_id = $app->request->post('user_id', 0);
}
}
It's pretty straigtward creating a global function that does that, but I'm looking to have that function on the slim $app instance.
You could try using https://github.com/slimphp/Slim-Middleware and ensuring that the requests with a JSON body contain the proper Content-Type: application/json header.
I have an interesting situation when calling the Shopify API. I use the standard procedure for calling the url and get the data, like this:
define('SHOPIFY_SHOP', 'myteststore.myshopify.com');
define('SHOPIFY_APP_API_KEY', 'xxxx');
define('SHOPIFY_APP_PASSWORD', 'yyy');
$shop_url = 'https://'.SHOPIFY_APP_API_KEY.':'.SHOPIFY_APP_PASSWORD.'#'.SHOPIFY_SHOP;
$response = Requests::get($shop_url.'/admin/products.json');
And I correctly get the response, parse the data and all works great. Now, when I put it to the actual server (Ubuntu 12.04), I noticed a weird message from the Spotify API:
[API] Invalid API key or access token (unrecognized login or wrong password)
I tried creating a new app, but still its the same. So the same file and the same set works on my machine, but not on the server. (only difference in the file is the path to requests library, require_once './Requests/library/Requests.php'; for Linux and require_once '..\Requests\library\Requests.php'; for Windows) As stated, I use the requests library and I assume there has to be some trick where the library (or something else) rewrites the URl and it doesn't get to Shopify correctly.
I tried using CURL with the URL directly, and it works that way as well. Can anyone point me what might be causing this?
Update: I moved to another library which solved the issue, but would like to know what was causing this since I had great experience with Requests up to this point.
I'm starting to use the same lib, and I stumbled upon something relevant right after finding this question:
https://github.com/rmccue/Requests/issues/142#issuecomment-147276906
Quoting relevant part:
This is an intentional part of the API design; in a typical use case,
you won't necessarily need data sent along with a request. Building
the URL for you is just a convenience.
Requests::get is a helper function designed to make GET requests
lightweight in the code, which is why there's no $data parameter
there. If you need to send data, use Requests::request instead
$response = Requests::request( 'http://httpbin.org/get', $headers, $data, Requests::GET, $options );
// GET is the default for type, and $options can be blank, so this can be shortened:
$response = Requests::request( 'http://httpbin.org/get', $headers, $data );
I couldn't figure why is this happening, it appears the Requests library is stripping the parameters from GET requests, so I moved to unirest library and this solved the issue.
I'm trying to get a JSON string from a page in my Laravel Project. Using this:
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
return View::make('adventuretime.marceline')
->with('json', $json)
->with('title', 'ICE KING')
->with('description', 'I am the Ice King')
->with('content', 'ice king');
But since I'm only using a localhost, I think this doesn't work that's why it doesn't output anything. I want to know what is the proper way for it to be flexible and be able to get the JSON string with any $url value using php?
Looking at the comments above, it is possible that the $url you are using is not valid, check it out by pointing your browser there and see what happens.
If you are sure that the $url is fine, but you still get the 404 Not Found error - verify that you have proper Laravel routing defined for that address. If the routes are fine, maybe you forgot to do
composer dump-autoload
after making modifications in your routes.php. If so, try the above and refresh the browser to see if it helps.
Furthermore, bear in mind that using your current function, you can submit only GET requests. What is more, this function might not be available for fetching remote urls, on some hosting servers due to security reasons. If you still want to use it, it'd be good to check
if($json !== FALSE)
before you process the $json response. If the file_get_contents fails it will return false.
Reffering to the part of your question
what is the proper way for it to be flexible and be able to get the JSON string with any $url
I'd suggest using cURL, as a standard and convenient way to fetch remote content. Using cURL you have better control over the process of sending the http request and receiving the "answer" it returns. Personaly, in my Laravel 4 apps I often use this package jyggen/curl. You can read the docs for it here: jyggen docs
If you are not satisfied with cURL and you want greater control try Guzzle As the authors state, Guzzle is a PHP HTTP client & framework for building RESTful web service clients.
I am attempting to write a RESTful service using CakePHP 2.3.5. So far I've successfully created the GET functions for the resource I'm working with. I can send a GET request to example.com/areas.json or to example.com/areas/1.json and it returns the data in my database.
However, I started trying to get the edit function working. I wrote a simple edit method that simply saved the incoming data from $this->request->data. I'm using Postman to test the functionality and sending raw JSON over PUT or POST to example.com/areas/1.json returns a message telling me that the data couldn't be saved. I made the method send me more information when it failed and it tells me that there is no incoming data in either $this->request->data or $this->data.
I've been searching the Internet for solutions to this or similar problems, but everything I have tried has failed so far. I've attempted disabling CSRF checks, disabling the SecurityComponent altogether, and multiple other fixes all involving the security. Changing any of those resulted in black holing the request.
Does anyone have any thoughts on what else I could try to get CakePHP to accept the JSON data into a request? I'll include my edit function below in case that helps.
public function edit($id)
{
$this->Area->id = $id;
$message['request-data'] = $this->request->data;
if ($this->Area->save($this->request->data)) {
$message['response'] = $this->Area->findById($id);
} else {
$message['response'] = "Error";
}
$this->set(array(
'message' => $message,
'_serialize' => array('message')
));
}
First, make sure the Content-Type of the request is application/json.
Second, CakePHP doesn't automatically decode the JSON payload; you have to do it manually. From the manual:
// Get JSON encoded data submitted to a PUT/POST action
$data = $this->request->input('json_decode');