Nested Request with JSON - laravel-5

I am trying to decode a request that comes from Twilio in Laravel 5.
It comes as application/x-www-form-urlencoded, but it has a JSON payload in it with data that I want to access called Memory.
How do I access it in Laravel?

Twilio developer evangelist here.
As you're looking at memory, I'm guessing you're using Twilio Autopilot. In that case, you should get the Memory field from the request and then parse it as JSON data.
$rawMemory = $request->input('Memory');
$memory = json_decode($rawMemory, true);

You probably just need to get the content from the request, which should contain the JSON.
$requestData = json_decode($request->getContent(), true);
dd($requestData);

Related

Getting the entire JSON request?

How can I read a JSON request?
$request->all(); //Uncaught ErrorException: Array to string conversion
This does not work.
$request->('title');
This works and gets the title param.
But how can I get the entire data set?
After some googleing I see there was once a method called:
$request->json()->all();
But this no longer appears to work.
Try doing this
dd(request()->all());
Since $request is an object of Request class, there is no property for $request->all()
"When sending JSON requests to your application, you may access the JSON data via the input method as long as the Content-Type header of the request is properly set to application/json.
You may even use "dot" syntax to dig into JSON arrays:"
$name = $request->input('user.name');
https://laravel.com/docs/5.5/requests#retrieving-input

Do I have to add an after filter to change all response content-type to json?

I'm using laravel 4.2, I've recently discovered that I'm not getting 'content-type' : 'application/json' on all my API requests that are JSON.
So...
was this supposed to work out of the box or am I supposed to add a filter by myself?
my options are either changing all the responses in my code that are json to Response::json()
or
the easiest solution is adding an after filter that checks if the response is json and changes the headers
json_decode($response->getContent());
if ( json_last_error() == JSON_ERROR_NONE ) {
$response->headers->set('Content-Type','application/json');
}
this seems to add some extra overhead for each request. Is there a better way?
Laravel already has what you need.
Response::json($your_data)
read more about it in docs
Laravel checks if your data implement JsonableInterface or ArrayObject or it's just array and convert it to json and set headers.

Magento getPost empty array

I have the following code (please excuse the bad coding, it's like that to debug):
$postData = Mage::app()->getRequest()->getPost();
if(!$postData)
{
$postData = $this->getRequest()->getPost();
}
if(!$postData)
{
$postData = $_POST;
}
As you can see, I am simply trying to get the HTTP POST values.
Here's the scenario:
From a HTTP POST Simulator, the data comes through
From the Shopify webhook, nothing comes through (just "Array()")
Shopify posting to PostCatcher shows a lot of data
Shopify is posting in JSON format.
Any ideas as to ahy I can't catch the POST array?
You cant get JSON post values by using simply $_POST or Mage::app()->getRequest()->getPost();. Just try this,
$value = json_decode(file_get_contents('php://input'));
print_r($value);
In one of my project I got the same error
Mage::app()->getRequest()->getPost(); was giving the blank values.
I was using one extension when I was submitting the form there was one description field for the manufacturer.
If it was having text content like from , select or some content similar to SQL commands.
It was not posting the form data.
The reason was the hosting provider had some settings for sanitizing the data to prevent the SQL injection.
I raised the ticket to the hosting provider and the problem was solved.
Before this I tried lot of coding stuff which was not required.
Basically
Mage::app()->getRequest()->getPost(); and $this->getRequest()->getPost(); are the same if you are in a controller.
They are also the same thing with $_POST with some additional filtering on the values.
So if you receive an empty array in any case you should receive an empty array for all cases.
Make sure the data is sent through POST.
Also try to see how $this->getRequest()->getParams() look like. Maybe Magento considers that the parameters are sent through _GET

Checking the type of POST data received in Sinatra

In my sinatra app i have a form which is used to submit data via a POST request to a url.The url also accepts json sent in a POST request.
Is there any way to determine in the handler if json data was received in the post or the data submitted was sent from the form ?
Thank You
When you send data via a Post request you will have data in your params Hash. So if there is a key there is a value, even if it's empty. So you can check for example via params[:json] if you have received something via json (assuming you call that parameter :json). The same goes for data. But then I'm not entirely sure if that's what you're asking for. Either way all data you get is handled via the params variable.
Assuming that JSON is sent via XHR call, you can make use of request.xhr? to check if the request is xhr.

How to use Qooxdoo's Stores to request an ajax response with a POST?

In the tutorial enter link description here they only show this:
var url = "http://api.twitter.com/1/statuses/public_timeline.json";
this.__store = new qx.data.store.Jsonp(url, null, "callback");
But I would need to communicate with my own server, so I've made some changes: (url and Jsonp to Json)
var url = "http://127.0.0.1:8000/";
this.__store = new qx.data.store.Json(url);
But I would need to be able to send some information to the server when the store make the request like:
{serviceToUseOnServer: 'articles', argument1: 'xxx'}
This is like a POST request, but I don't really know how to send that data to the server using qooxdoo's Store models. Please don't tell me to use GET and encode all that data in an url.
You got the possibility to configure the request object used to send the data. Just add a delegate to the store and implement the configureRequest method [1].
[1] http://demo.qooxdoo.org/current/apiviewer/#qx.data.store.IStoreDelegate~configureRequest

Resources