Getting the entire JSON request? - laravel

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

Related

Nested Request with JSON

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);

Laravel $request->all() correctly returns data, but $_POST completely empty

I am making an ajax post request to the server, posting json data. In firebug I can see the network post call going through along with the json data.
In Laravel I was trying to do a simple var dump of the $_POST data and have just wasted a fair bit of time being confused as to why this should be completely empty. However, when I use the Request facade, my data is there.
ie. this just gives me an empty array:
public function test(){
Log::info($_POST);
}
...yet this prints my data, as I expect:
public function test(Request $request){
Log::info($request->all());
}
Why?
Edit
Thanks, #Webdesigner. The http verb is definitely post, as my method is called in my routes file via
Route::post('/image-upload', 'EntryController#test'); // Note "post" verb
I don't think $request->post() is valid in Laravel 5.4 as this throws an BadMethodCallException: Method post does not exist. error. However, I can confirm that
Log::info($request->method()); // POST
also tells me the method is post.
Very strange. I guess you're right that some part of the app is overwriting the $_POST global, though I have no idea why/where/how. Probably not relevant, but this call is being made from Angular 4.
Thanks for your help anyway!
This is not the normal behavior of Laravel. I tested this on a fresh Laravel 5.5 site and just did a Form submit and an Ajax POST request to the same Route.
Both give me the same result. A POST Request should have at least the CSRF Token as _token with a value.
One other point is $request->all() is not only the the content of $_POST so to have a fair compression you should try $request->post().
BTW only because you did a POST request do not mean that the data is send by the POST Method, it could be that the data you see in $request->all() is from $_GET and $_COOKIE, etc and only the Method was a POST.
Last but not least there it the option that some part of your APP is deleting the content of the Superglobal Variables. $_POST and the others are not like constants, so they can be changed during runtime e.g. $_POST = [];
I don't thing that there is a difference in Laravel 5.4.27.

Postman send value in laravel is null

I'm trying to send value in laravel controller using postman but it send null value.
UserController:
Route:
Postman:
There is no problem in get method
I am not sure which one are you accessing since you set the value in header but your variable named input. So, I will just provide both methods since it should be helpful!
There are two different method for accessing the body of the request or accessing the header of the request
Accessing the header request
$val = $request->header('val');
Accessing the body request
$val = $request->input('val');

temporarily change $http.defaults.transformResponse

How can I customize $http in angularjs such that it will accept strings as its response in a $http.post call? Right now, when I do a $http.post call, my response is in string but angularjs by default uses JSON therefore I get an error. Right now I have something along the lines of
function getResponseURL(response) {
//this will convert the response to string
return response;
}
$http.defaults.transformResponse = [];
$http.defaults.transformResponse.unshift(getResponseURL);
However if I use the code above, any $http.post calls after that call uses string. I want it to use the original default JSON format. How can I go about into just temporarily changing the response to string for this one call but the rest stay as JSON type as a response?
Why not only register that transform for ONLY that request?
Angular js $http docs
If you wish
override the request/response transformations only for a single
request then provide transformRequest and/or transformResponse
properties on the configuration object passed into $http.

How to send Object via Ajax to Servlet

How do I send an element of type object via ajax to a servlet?
In the ajax I am passing the value as follows below:
data: { mapList : mapLists }
To get the value in the Servlet am doing follows below:
Object o = request.getAttribute("mapList");
System.out.println(o);
However, the returned value is always null. What should I do to get around this problem?
Change your ajax datas by :
data: { 'mapList' : mapLists }
On HTTP GET or POST requests you can only send a list of key/value pairs as parameters to the server so you will have to manually serialize your object to send its attributes in this format.
You should better use HttpServletRequest.getParameter(String) in place of HttpServletRequest.getAttribute(String). Also, what you get as an HTTP GET/POST parameter will always be received in the servlet as a String.
I assume that you are using jQuery to send the ajax request. I also asume that your mapLists variable is a json object. As far as I know, jQuery doesn't automatically convert a json object to a key/value pair HTTP parameter list so you will have to do it by yourself and then parse it back in the servlet. You can use JSON.stringify() to convert your json object or you can serialize it manually.

Resources