When I was trying in browser to send a GET request with data from bootstrap datatable ajax to a C# MVC controller method, not all the data that was send was mapped to the model object. Some properties in the model object still remained with null/default value.
I verified the property names and no issues in that. But when I did the same thing in postman app the data was successfully bound to the model object.
Two things that I noted in the postman app is that:
The request data is not send in the query string parameter rather in
the request body with still the method type as GET (I don't know
how that is possible), but in browser as usual the data was URL
encoded and was sent as query string parameter.
In post man app the request headers also contains "Content-Type"
header with "application/json" as value.
So I also tried with "Content-Type" header with "application/json" but still no use the data was not bound.
Finally I chose the common way, I just set the "Content-Type" header with "application/json", stringified the data with JSON.stringify and I set the type as "POST" and it successfully got mapped.
What I got struggling is, how does the postman app alone can send a GET request like that with data in the request body.
If anyone has idea about this please tell me that would help me to have an idea about the HTTP requests.
1.) PostMan - GET Request Format
2.) PostMan - Get Request Network Data
3.) PostMan - Get Request Network Data - Raw Format
4.) Backend - Contoller Received Data
1.) Browser - GET Request Format
2.) Browser - Query String Parameters
3.) Backend - Contoller Received Data
Related
My requirement is to accept xml data file that may be gziped in HTTPS POST request and request may be chunked.
Request body WILL have the type of application/.xml.
I am using Postman to make request and java spring to write the controller and rest of the api code.
My questions is how will my request will look like, either it will be form data with xml file in input or raw body with xml selected in postman.
I am finding it hard to make a curl for it.
Here is the example request requirement:
Problem:
I am trying to get the content / body from a text/plain POST request in a Controller in Laravel 8.
Nothing in the docs or numerous google searches worked.
I have tried:
$request->input();
$request->all();
$request->json();
json_encode($request);
All of these seem to show that the request is completely empty.
Context
This may not be relevant to the solution, but might help other's who are trying to google this problem: The request I am trying to handle with my controller is being sent by navigator.sendBeacon on the client side. The request body is actually stringified JSON, but sendBeacon does not allow you to send requests with content-type JSON. Devtools show the content-type header for this request as 'text/plain'.
My Answer:
Use $request->getContent() to get the content of a text/plain HTTP request.
And then, if you are in a situation like mine where the text is actually JSON, use json_decode($request->getContent(),true) to get a standard PHP array from the content which you can use in your Controller.
It turned out to be quite simple, but the information was not in the docs, or in any online searches so I thought it would be worthwhile to post to SO anyways...
You can send the data as JSON like this instead
data = new Blob([JSON.stringify(data)], {type : 'application/json'})
navigator.sendBeacon('/your/route/here', data)
HI everyone I need a help regarding to fix the 500 internal server error
Please check my below attached requests and correlation and response image
Please tell me if I was wrong in anywhere I keep trying to do the scripts to run and the result was 500 error.
Redirect request:
Https request1:
Data correlation:
Passing parameters:
Sending request details:
Response message:
Looking into Content-Type header of your request it appears that you should be sending a JSON while you're sending parameters in form of name-value pairs
I think you should switch to Body Data tab in the HTTP Request sampler and put your parameters there like:
Also double check your API contract, it might be the case you need to change the method to POST
More information: REST API Testing - How to Do it Right
I fetching data from an api end point.
When I use postman to send the request, I get an array of of objects (non empty).
However when I simply request it via the browser I get an array of empty objects...
Why is this happening?
You need to pass authorization token and Content-Type headers while making a request through the browser.
Browser only able to make a get request, it doesn't set header value(s) directly for you. You need to add extension of chrome like ModHeader, which will help you to set request and response header.
Moreover, I don't think you need to worry about it, as I assume you will need application internal call instead.
Please somebody explain this info from Laravel docs "When using the validate method during an AJAX request, Laravel will not generate a redirect response. Instead, Laravel generates a JSON response containing all of the validation errors. This JSON response will be sent with a 422 HTTP status code."
What does "during an AJAX request" exactly mean here?
If I'm set like this:
User submits input from a form in the view.
The route calls the method for post in the controller
The request is validated in the controller
case 1) Request passes validation and input is stored in the DB -> Response is returned as JSON for a script that updates the view on the fly.
case 2) Request doesn't pass validation, what is returned here? I think a redirect and if not how can you check if validation has failed to return a JSON instead?
And is this scenario similar to what the docs talk about? If not then what?
The response to an AJAX call in this case is a JSON string
Case 1: as you said
Case 2: A JSON String with the error messages. Look into your Chrome dev tools into the Network tab, there you'll see the exact responses.
When handling a validation failure, laravel automatically generates an error response. Normally this will redirect the user back to their form, but in the case of an AJAX request it will instead return a JSON response with the details of what failed validation.
Laravel relies on the symfony request object to detect AJAX requests, specifically this line:
return 'XMLHttpRequest' == $this->headers->get('X-Requested-With');
If you are encountering issues with your requests not being treated as AJAX, make sure your client is properly setting the X-Requested-With header