How can I get the json data from a GET request to a Django Rest route. I am making a GET request to a url and have data in the body of Postman as JSON(application/json). How do I get the data from this request in Django Rest Framework, in my view.
My view sends back data based on what is passed in the JSON data.
I do not want to put the data in the url as a query_param. I will be sending a lot of parameters in the JSON data body, so concatenating to the url is not what I want.
It is not possible to send json format with get request you have to use post request to send data else use query param with get request.
For more info see this django request and response tutorial
Related
In websites, data can be sent using an HTML form to POST /createUser with a payload in the body, and expect a redirect to some page (for example, to a newly created user) in response. Or you can send the same request using an AJAX request (fetch, axios) and expect a JSON response (in which, for example, the same link to a new user can be a field {”redirectTo”: “/createUser”} along with another response payload).
How to distinguish these requests in the controller from the MVC pattern in order to respond with what the client expects? I see these options:
Create different URLs - one for a request with a redirect, the other with a response in JSON. But here the problem is that they have the same logic, only the view-representation differs.
Common URL, but an additional field in the body or header, in which the client himself will explicitly indicate what kind of response he expects.
A common URL, but the server itself determines from the request headers (for example, Content-Type) where the request was made from and what the client expects. But aren't there some edge scenarios where the server might guess wrong?
Is there a way I can extract the request body (JSON) from the spring cloud gateway?. I went through a few approaches like RequestBodyrewrite and all, but as far as I understood it is used for modifying the response body. In my case, I wanted to get the request body and do some logic there and send the data via headers to the underlying microservices. Right now I am getting the request body with the attribute "cachedRequestBodyObject" with the help of the request body predicator.
readBody(SomeClass.class, s-> true)
I don't think I am not fully making use of the request body predicator here. We would also get this as part of the getRequestBody() method but it has a data buffer as a return type, so not sure how to get the JSON details from the data buffer. Is there a way better approach to do this ?..
Thanks in Advance.
So these three methods are in the Laravel docs for testing if the request is for JSON.
I basically want to return JSON for any request that is requesting JSON or AJAX.
Request::ajax()
Request::isJson()
Request::wantsJson()
Can I use any of these? Do they overlap?
Not all AJAX requests expect a JSON response, so utilizing request()->ajax() is useful where you want to determine if the request was an XmlHttpRequest or not, but the response doesn't care about JSON or not.
Not all requests that contain JSON expect a JSON response. so if you don't care about whether or not the response wants JSON back, but want to determine if JSON was sent in the request, then isJson() is useful for you.
Not all requests that want JSON responses are AJAX driven, so wantsJson is useful in the case where you want to return JSON data, but you don't care how the request came to your server.
Based on these descriptions, make assertions and choose the proper functions.
You can use Request::expectsJson(). It returns true if request is ajax or wants json.
request()->ajax() is usefull if you want to check if request is only Ajax
Request::wantsJson() is usefull if you want to check if request is only Json
Request::expectsJson() is usefull if you want to check if request is either Ajax or Wants Json
I'm sending POST request on Jmeter with one of the parameters with json format. But JMeter is not sending it as a raw as you can see below. It's encoding the json and requesting with % and numbers... **msg** should be sending raw json.
I tried various options with encoding but none is working. How can I send json as a raw as a request parameter?
POST data:
trans_code=KycPartnerSender&version=1.0&pkg_id=1234123×tamp=123123&**msg**=%7B%22sId%22%3A%7B%22weChatId%22%3A%22jnaS001%22%7D%2C%22sInfo%22%3A%7B%22firstName%22%3A%22jnaSender001%22%2C%22middleName%22%3A%22%22%2C%22lastName%22%3A%22QA%22%2C%22dob%22%3A%221958-01-31%22%2C%22email%22%3A%22001%40ZB.CA%22%2C%22taxId%22%3A%22467456789%22%2C%22mobilePhone%22%3A%224161231234%22%2C%22workPhone%22%3A%224161231234%22%2C%22citizenCountryCode%22%3A%22CA%22%2C%22residenceCountryCode%22%3A%22CA%22%7D%2C%22sAddress%22%3A%7B%22line1%22%3A%22123+Yonge+Street%22%2C%22line2%22%3A%22Suite+1902%22%2C%22city%22%3A%22Toronto%22%2C%22countryRegionCode%22%3A%22ON%22%2C%22postalCode%22%3A%22M2M2M2%22%2C%22countryCode%22%3A%22CA%22%7D%2C%22sFundingSource%22%3A%7B%22accountNumber%22%3A%22123456789%22%2C%22accountHolderName%22%3A%22JNA+SENDER%22%2C%22routingNumber%22%3A%2200402652%22%7D%2C%22rId%22%3A%7B%22weChatId%22%3A%22jnaR001%22%7D%2C%22rInfo%22%3A%7B%22firstName%22%3A%22jnaReceiver%22%2C%22middleName%22%3A%22X%22%2C%22lastName%22%3A%22QA%22%2C%22dob%22%3A%221970-10-31%22%2C%22email%22%3A%22002%40ZB.CA%22%2C%22taxId%22%3A%22789456123%22%2C%22mobilePhone%22%3A%224161231234%22%2C%22workPhone%22%3A%224161231234%22%2C%22citizenCountryCode%22%3A%22CA%22%2C%22residenceCountryCode%22%3A%22CA%22%2C%22employmentStatus%22%3A%22Full+Time%22%2C%22occupationCode%22%3A%22102%22%2C%22industryCode%22%3A%22101%22%7D%2C%22rAddress%22%3A%7B%22line1%22%3A%22123+Bloor%22%2C%22line2%22%3A%22Suite+1900%22%2C%22city%22%3A%22Toronto%22%2C%22countryRegionCode%22%3A%22ON%22%2C%22postalCode%22%3A%22M2M2M2%22%2C%22countryCode%22%3A%22CA%22%7D%2C%22rFundingSource%22%3A%7B%22accountNumber%22%3A%22987654321%22%2C%22accountHolderName%22%3A%22JNA+RECEIVER%22%2C%22routingNumber%22%3A%2200402652%22%7D%7D
Instead of using Parameters table, select the Raw Body tab and just put your json body.
Of course it only works with Post method (not Get) so it should be ok for you.
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.