Distinguishing between GET and POST data in CherryPy? - python

I've been deciding between Python web frameworks for a project of mine and I've really liked how lightweight, flexible, and concise CherryPy is compared to others. The only problem I'm having is I can't find any documentation on how to distinguish between data sent via GET and via POST.
For example, I don't want users to be able to provide their login credentials through a GET request (http://example.com/login?username=user&password=pass) but, according to CherryPy's tutorial, all data is sent as method parameters, no matter what HTTP method they're sent as. Is there some way to say I only want the POST data or do I have to use MethodDispatcher?
Thanks!

See the docs.
A string containing the HTTP method, such as "GET" or "POST". Set
in the "run" phase.
looks like checking cherrypy.request.method is what you want to do.

Related

HTTPS Post data from Lambda function for an Alexa Skill

I know that there are methods for sending a GET request with a vanilla Alexa Lambda function, but does the same thing exist for POSTing data to an HTTPS URL?
I want to post data to a URL when the user fires a certain intent in my skill.
Yes, there are.
This is a tutorial on creating POST method with JSON payload from Amazon Doc
Personally, I would prefer to modulate and separate out the code dealing with HTTP out from the Alexa Skill, and put it into another Lambda function. Of course, it's totally optional, but here are my reasons:
modularity for better debugging
cleaner code if you have multiple skill states.
If you are looking for some code example, I use to have a project that did similar job, hope you'll find the documentation helpful in some way.

Python is there a way to read custom headers and send a response back

Ok so I am new to python as a whole. That said I don't know what I'm really looking for to ask this question properly. I know this has to be possible though. I want to ask this before really digging in and finding out I did something wrong and have to do it all over.
All in all what I want to know is, from the front end of my stack I want to pass down custom HTTP headers (which I can do with my Ajax calls, currently). The question is how do I actually read said headers? Similarly how can I pass back up from the server custom headers via python.
You can access custom header in Django view:
request.META.get("Custom_Header")
For django:
How to add an HTTP header to all Django responses
You can definitely do it in the front end. You can do it with Javascript's native XMLHttpRequest, the newer fetch API, jQuery, or some other library (like axios).

API endpoint confusion

Disclaimer: I am new to working with APIs
I am working on leveraging gimbals API and am trying to figure out what exactly end points are? I realize that they link to a server, but how exactly are they used in development?
Are the endpoints used to link to specific sources of data?
I am using python(django) and it would be great to understand exactly how access and or change information on gimbals end.
PS- When looking at the gimbal api, I noticed that they have a REST api and some other mobile stuff going on. If I am building a web platform, I would only be interested in the REST API portion correct?
An endpoint in a RESTful API is generally just a URL. The URL represents some kind of resource. If it was an order processing API, the resources would be things like customers, orders, etc.
You interact with these resources by making HTTP requests of various sorts; GET if you want to know the content of a resource, POST if you want to change something, and so on. Have a look at this for basic information on REST and web APIs.
You don't need Django to interact with a RESTful API that someone else provides. All you really need is python's urllib, and maybe the json module, if they're sending the data in JSON. The REST API they provide is probably the main thing they want developers using, but if they have multiple APIs then it's hard to say which one is right for you without understanding the application better.

mod python, get POST parameters from request object

Is there any basic example that shows how to get POST parameters from the request in mod python custom handler. I have no trouble in GET requests, where I get my arguments from request.args, BUT if method is POST, request.args is None.
Thanks.
request.args stores query string parameters, as mentioned in the documentation.
If you want to get POST variables, you can always read the body of the request (request.read()) and parse it (urldecode in your case).
But keep in mind that, as mentioned on the official mod_python homepage:
Current State of Mod_Python
Currently mod_python is not under active development. This does not mean that it is "dead" as some people have claimed. It smiply means that the code and the project are mature enough when very little is required to maintain it.
Which means you may be better off using something more modern, like mod_wsgi.

Is www.example.com/post/21/edit a RESTful URI? I think I know the answer, but have another question

I'm almost afraid to post this question, there has to be an obvious answer I've overlooked, but here I go:
Context: I am creating a blog for educational purposes (want to learn python and web.py). I've decided that my blog have posts, so I've created a Post class. I've also decided that posts can be created, read, updated, or deleted (so CRUD). So in my Post class, I've created methods that respond to POST, GET, PUT, and DELETE HTTP methods). So far so good.
The current problem I'm having is a conceptual one, I know that sending a PUT HTTP message (with an edited Post) to, e.g., /post/52 should update post with id 52 with the body contents of the HTTP message.
What I do not know is how to conceptually correctly serve the (HTML) edit page.
Will doing it like this: /post/52/edit violate the idea of URI, as 'edit' is not a resource, but an action?
On the other side though, could it be considered a resource since all that URI will respond to is a GET method, that will only return an HTML page?
So my ultimate question is this: How do I serve an HTML page intended for user editing in a RESTful manner?
Another RESTful approach is to use the query string for modifiers: /post/52?edit=1
Also, don't get too hung up on the purity of the REST model. If your app doesn't fit neatly into the model, break the rules.
There is no such thing as a RESTful URI. It is false concept as URIs should be completely opaque to the client.
If it helps you to properly implement the HTTP uniform interface by avoiding verbs in your URIs then that's great, but don't feel constrained by what your URI looks like. It is very limiting to think of resource modeling as type of data modelling. A RESTful system usually needs to do way more than just CRUD operations, so you need to be creative about what resources you make available in your system.
If you create a URL and dereferencing it returns a 200 status code, then that URL refers to a resource. If you create another URL and it also returns a 200, then that is a difference resource.
That means:
http://example.org/customer/10.xml
http://example.org/customer/10.json
http://example.org/customer/10?format=xml
http://example.org/customer/10?format=json
are 4 different resources, and
http://example.org/customers
http://example.org/customers?closed=true
http://example.org/customers?page=2&pagelength=20
are also different resources.
Therefore to answer your question, if you do
GET /post/52/edit
and it returns a 200 status code and a representation, then it must be a resource.
Instead of calling it /post/52/edit, what if you called it /post/52/editor?
Now it is a resource. Dilemma averted.
I don't think /object/id/action is part of the REST specification.
Is your editor going to be a generic thing for all objects ? Then maybe your URL should look like
/editor/object/id
The action is an HTTP Verb ( GET,PUT,DELETE,POST ) and is supposed to be a part of the HTTP request and not part of the URL. For a better summary, check out this Wikipedia article on RESTful_web_services.

Categories