I want to send information back-end via JS to update the database based on user response, but I don't need a JSON response or other HTTP rendering done.
It's a test type app so all I'm sending back is a single identifier (integer) and boolean value to indicate pass/fail on their response.
Currently I am using a fetch(url/${identifier}/bool) and all works as intended with the data management, but Python/Django throws an error as I am not returning any value.
So I have two questions.
Is fetch the right way of doing this as I am not actually expecting a response and I am doing no manipulation JS side with the data after calling it.
How can I stop Django from expecting a response on this as I intend to run the call as a simple function rather than an API call with JSON response sent back.
Thank you for the help.
Fixed the issue by simply using
return HttpResponse(status = 200)
Related
I tried rendering the response of multiple API in a single template but since once one view can we passed in the path, i am unable to think of what i can do
I tried keeping the two functions to call the API inside a class but i got GET 405 error
1st image
2nd image
First of all cant say anything without seeing the code but I think you can create a dictionary and pass it as context in response. Save the multiple api and pass it into dictionary
mydict = {
"api1":api_data,
"api2":api_data,
}
pass this as context in response
I have a REST API that has a database with table with two columns, product_id and server_id, that it serves product_ids to specific servers which request the data(based on the server_id from table).
Let's say I have three servers with server_ids 1,2 and 3.
My design is like this: /products/server_id/1 and with GET request I get json list of product_ids with server_id = 1, similarly /products/server_id/2 would output list of product_ids for server_id = 2.
Should I remove these routes and make a requirement to send POST request with instructions to receive product_ids for specific server_id in /products route only?
For example sending payload {"server_id":1} would yield a response of list of product_ids for server_id = 1.
Should I remove these routes and make a requirement to send POST request with instructions to receive product_ids for specific server_id in /products route only?
Not usually, no.
GET communicates to general purpose components that the semantics of the request message are effectively read only (see "safe"). That affordance alone makes a number of things possible; for instance, spiders can crawl and index your API, just as they would for a web site. User agents can "pre-fetch" resources, and so on.
All of that goes right out the window when you decide to use POST.
Furthermore, the URI itself serves a number of useful purposes - caches use the URI as the primary key for matching a request. Therefore we can reduce the load on the origin server by re-using representations have have been stored using a specific identifier. We can also perform magic like sticking that URI into an email message, without the context of any specific HTTP request, and the receiver of the message will be able to GET that identifier and fetch the resource we intend.
Again, we lose all of that when the identifying information is in the request payload, rather than in the identifier metadata where it belongs.
That said, we sometimes do use the payload for identifying information, as a work around: for example, if we need so much identifying information that we start seeing 414 URI Too Long responses, then we may need to change our interaction protocol to use a POST request with the identifying information in the payload (losing, as above, the advantages of using GET).
An online example of this might be something like an HTML validator, that accepts a candidate document and returns a representation of the problems found. That's effectively a read only action, but in the general case an HTML document is too long to comfortably fit in the target-uri of an HTTP request.
So we punt.
In a hypermedia api, like those used on the world wide web, we can get away with it, because the HTTP method to use is provided by the server as part of the metadata of the form itself. You as the client don't need to know the server's preferred semantics, you just need to know how to process the form data.
For instance, as I type this answer into my browser, I don't need to know what the target URI is, or what HTTP method is going to be used, because the browser already knows what to do (based on the HTML and whatever scripts are running "on demand").
In REST APIs, POST requests should only be used in order to create new resource, so in order to retrieve data from server, the best practice is to perform a GET request.
If you want to load products 1,2,4,8 on server 9 for example, you can use this kind of request :
GET https://website/servers/9/products/1,2,4,8
On server side, if products value contains a coma separated list, then return an array with all results, if not return just an array with only one item in order to keep consistency between calls.
In case you need to get all products, you can keep only the following url :
GET https://website/servers/9/products
As there is no id provided in products parameter, then the server should return all existing products for requested server parameter.
Note : in case of big amount of results, they must by paginated.
So I have been building a chatbot powered by RASA stack (open source).
After creating the bot, I wanted to integrate it with our web application. Now I'm able get responses from my RASA core but I'm in a problem. I'm passing a unique user_id in the GET request which i need to fetch inside a python function and call an external API to my Database. But I don't know how to fetch that parameter out from GET request. here are some details.
My GET request: (I uploaded my bot on AWS server)
http://my_ip_.amazonaws.com:5005/conversations/27/respond?q=%27Hi
So my unique id is 27 which i want to fetch inside a python function.
and the response i'm getting by this request :
[{“recipient_id”:“27”,“text”:“Hey! What can I do for you?”}]
As you can see I passed the GET request in postman and got this response from my RASA CHATBOT but I want to track this user-id 27.
So my question is how can I track this id? Or maybe you guys can suggest me another way to do it.
Thanks for your help in advance :) My first post BTW :)
[Please ask me anything if you feel this question is missing something]
So you are receiving the following data from the request:
[{“recipient_id”:“27”,“text”:“Hey! What can I do for you?”}].
If it's a list:
just use:
response = [{“recipient_id”:“27”,“text”:“Hey! What can I do for you?”}]
recepient_id = response[0]['recipient_id']
If it's a dict:
response = {“recipient_id”:“27”,“text”:“Hey! What can I do for you?”}
recepient_id = response['recipient_id']
so I solved this issue by using tracker data. Actually, I had to link tracker dictionary to my custom action file so that I can access the sender_id and other slot values.
I used:
user_id = tracker.sender_id
parameters ={}
parameters = {"user_id": user_id}
then it follows by my post request and it works! Thanks
I want to make a JSON request with the Python library requests where I only obtain certain JSON objects.
I know that it is really easy to process the JSON object obtained to only focus in the needed information, but that would throttle the request efficiency (in case it is done repeatedly).
As said, I know this is a possibility:
url = 'www.foo.com'
r = requests.get(url).json()
#Do something with r[3]['data4'], the only one who is going to be used.
But how could I directly only obtain r[3]['data4'] from the request?
Short Answer
To answer your question no, you can't but to understand why you need to know what is happening behind the scenes.
Behind the scenes
When you make a request such as r = requests.get('www.foo.bar') you are making a request to the server and you are viewing the result of that request when you do r.json(). This means that you cannot just get r[3]['data'] as you are parsing what the server sends to you unless the server only sends r[3]['data']. It may be possible to filter out everything else apart from that in the response processing but I am unaware of how to do it.
You can't, if the server does not allow it. If the target server allows you to specify fields you want then you can send that field list in your request and server will return you only those fields in JSON. Otherwise your will have to parse full JSON response and get your desired fields.
The variable cherrypy.request.params as it is described in the API contains the query string and the POST variables in a dictionary. However combing over this, it seems that it contains every variable received after processing the full request URI to pull the GET data. This then becomes indistinguishable from POST data in the dictionary.
There seems to be no way to tell the difference, or perhaps I am wrong.
Can someone please enlighten me as to how to use purely posted data and ignore any data in the query string beyond the request URI. And yes I am aware I can find out whether it was a POST or GET request but this does not stop forgery in requests to URIs containing GET data in a POST request.
>http://localhost:8080/testURL/part2?test=1
>POST username = test
"cherrypy.request.params" has 2 variables
test = 1
username=test
The docs aren't very clear on this point, but starting in CherryPy 3.2, you can reference request.body.params to obtain just the POST/PUT params. In 3.2 and below, try request.body_params. See http://docs.cherrypy.org/dev/refman/_cprequest.html#cherrypy._cprequest.Request.body_params