How to submit a request by POST in Pyramid? - python

In Pyramid, send a request by GET can be done by create URL like this:
#view_config(route_name="test")
def test(request):
return HTTPFound("http://test.com/redirect_to_another_test?a=1")
But it seems that the HTTPFound can't do that by POST, then how can I do that?
Does anyone have ideas about this? Thanks!

You can't do this in Pyramid or any other Server-Side Framework or Language.
Your example code isn't showing a form submission, the code is showing a HTTP redirect. It is instructing the Browser to visit another URL, or in other words, telling the browser to resubmit the request.
This Stack Overflow question discusses this same concept, although in ASP not Python - Response.Redirect with POST instead of Get?
If you were to "submit a request" in Pyramid via GET or POST, you would have to use a library like urllib2, requests, or similar. In those instances, the libraries would have the Pyramid server act as the "submitter" of the request.
If you want to have the User/web-broswer submit the request by POST, you would have to do some fancy footwork/trickery to make the browser do that.
Possible ways to accomplish that would include:
use JavaScript AJAX form submission, and return an error code or instruction via JSON telling your JavaScript library to resubmit the same form via POST.
redirect the user to a GET page which has the form-data filled out (via the GET arguments) , and use javascript to resubmit the form via POST onload
You can't, in any server side language, tell the browser to resubmit a request by POST. Browsers don't work like that.
You should also know that "Most Browsers" will generally interpret any redirect request to be fetched via GET -- even if the original was a POST. That's not to spec - certain redirect codes are supposed to keep a POST a POST - however browsers don't follow all the specs. There is also no HTTP status code or command to purposefully switch from GET to POST .

Related

Django URL does not see the link and its POST request

I made a project, but in it you need to get a special token from the VK social network. I made the token pass along with the link. She looks like this:
http://127.0.0.1:8000/vk/auth#access_token=7138dcd74f5da5e557943b955bbfbd9a62811da7874067e5fa0edef1ca8680216755be16&expires_in=86400&user_id=397697636
But the problem is that the django cannot see this link. I tried to look at it in a post request, get request, but everything is empty there. I tried to make it come not as a request but as a link, it is like this:
http://127.0.0.1:8000/vk/auth #access_token=7138dcd74f5da5e557943b955bbfbd9a62811da7874067e5fa0edef1ca8680216755be16&expires_in=86400&user_id=397697636
But the django does not want to read the space. Who can help
I think there is a confusion between a query string (get params) that follows a ? and a fragment (the text, that follows a #)
What follows the # is not sent to the server (and thus not received by Django) it is only useful to the web browser and to the javascript that is executed on the browser , which can use it to update parts of the screen. use it as virtual urls / bookmarks for one page web applications.
The javascript can of course also trigger AJAX requests using that data, but that's up to the javascript
If you write however http://127.0.0.1:8000/vk/auth?access_token=7138dcd74f5da5e557943b955bbfbd9a62811da7874067e5fa0edef1ca8680216755be16&expires_in=86400&user_id=397697636 (you replace # with ?)
Then you can receive the information in your django view with
request.GET["access_token"], request.GET["expires_in"] and request.GET["user_id"]
If it is really a #, then your javascript should parse whatever follows the # and make the according AJAX requests to the server to send / validate the token.
For another question about fragments, refer for example to Is the URL fragment identifier sent to the server?

Django redirect to a view with GET arguments set

I'm having a hard time understanding the Django System of views and templates. All I want to do is inform the user of the status of his request- he pushes a button and gets an infobox with a message. The button in the Dashboard is at the root of the URL and sends a POST request to the Django Web App at book/.
In the view bound to this URL, I check if the booking is valid and want to inform the user (without the use of javascript) about the result. I wanted to send back a HTTP redirect to /?response=success or /?response=failed.
I've tried to give the view of the dashboard arguments and changed the URL regex but this did not lead where I want it to go. Currently, it's just
return redirect('dashboard')
and the URL conf is:
...
url(r'^$', app.views.dashboard, name='dashboard'),
url(r'^book/$', app.views.book, name='book'),
...
I really like Django - it's really easy to use. But in simple cases like this, it just drives me crazy. I would appreciate any kind of help.
Kind regards
Eric
You could just use the messages framework - that's what it's for.
Oh and yes: if that's just for displaying informations (no side effect on the server), you should use a GET request - POST is for submitting data for processing by the server.

Using python to parse a webpage that is already open

From this question, the last responder seems to think that it is possible to use python to open a webpage, let me sign in manually, go through a bunch of menus then let the python parse the page when I get where I want. The website has a weird sign in procedure so using requests and passing a user name and password will not be sufficient.
However it seems from this question that it's not a possibility.
SO the question is, is it possible? if so, do you know of some example code out there?
The way to approach this problem is when you login normally have the developer tools next to you and see what the request is sending.
When logging in to bandcamp the XHR request that's being sent is the following:
From that response you can see that an identity cookie is being sent. That's probably how they identify that you are logged in. So when you've got that cookie set you would be authorized to view logged in pages.
So in your program you could login normally using requests, save the cookie in a variable and then apply the cookie to further requests using requests.
Of course login procedures and how this authorization mechanism works may differ, but that's the general gist of it.
So when do you actually need selenium? You need it if a lot of the things are being rendered by javascript. requests is only able to get the html. So if the menus and such is rendered with javascript you won't ever be able to see that information using requests.

How to redirect user to previous page when Logout?

I am making a webapp in GAE using python, and today I am required to write a logout function that redirects the user to the previous page he was seeing.
Logging out is fairly easy. Since I deal with logging in and logging out by using cookies, all I have to do is to delete the loggin cookie for the user.
The real problem here is redirecting the user to the previous page he was seeing. How do know what he was doing? Does webapp2 help with this at all?
The browser usually sends along the page the user came from, with the HTTP Referer (sic) header.
Because of privacy concerns, not all browsers send this, or they may falsify it or only send if the next page requested is in the same domain. But it is still the most common method of redirecting a user back to the previous page.
For webapp2, you could use something like:
referrer = self.request.headers.get('referer')
if referrer:
return self.redirect(referrer)
return self.redirect_to('home')
e.g. use the Referer header and fall back to a sensible default like the homepage.

Login to site programatically using python

I am wondering on how to log in to specific site, however no luck so far.
The way it happens on browser is that you click on button, it triggers jQuery AJAX request to /ajax/authorize_ajax.html with post variables login and pass. When it returns result = true it reloads document and you are logged in.
When I go to /ajax/authorize_ajax.html on my browser it gives me {"data": [{"result":false}]} in response. Using C# I did went to this address and posted login and pass and it gave me {"data": [{"result":true}]} in response. However then, of course, when I go back to main folder of the website I'm not logged in.
Can anyone help me solve this problem? I think that cookies are set via javascript, is it even possible in that case? I did some research and all I could do is this, please help me to get around with this problem. Used urllib in python and web libraries in .NET.
EDIT 0
It is setting cookie in response headers. SID, PATH & DOMAIN.
Example: sid=bf32b9ff0dfd24059665bf1d767ad401; path=/; domain=site
I don't know how to save this cookie and go back to / using this cookie. I've never done anything like this before, can someone give me some example using python?
EDIT 1
All done, thanks to this post - How to use Python to login to a webpage and retrieve cookies for later usage?
Here's a blog post I did a while ago about using an HttpWebRequest to post to a site when cookies are involved:
http://crazorsharp.blogspot.com/2009/06/c-html-screen-scraping-part-2.html
The idea is, when you get a Response using the HttpWebRequest, you can get access to the Cookies that are sent down. For every subsequent request, you can new up a CookieContainer on the request object, and add the cookies that you got into that container.

Categories

Resources