In GAE (Python), using the webApp Framework, calling self.redirect('some_url') redirects the user to that URL via the GET method. Is it possible to do a (redirect) via the POST method with some parameters as well?
If possible, how?
Thanks!
This is not possible due to how most clients implement redirection [1]:
However, most existing user agent implementations treat 302 as if it
were a 303 response, performing a GET on the Location field-value regardless
of the original request method.
So you must use a workaround (like simply calling the method post() from the RequestHandler) or forget the idea.
[1] http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2
You can pass parameters. Here is an example:
Let's say you have a main page and you want to POST to '/success'. Usually, you may use this way:
self.redirect('/sucess')
But if you want to pass some parameters from the main page to /success page, like username for example, you can modify the code to this:
self.redirect('/sucess?username=' + username)
In this way, you successfully passed the username value into the URL. In /success page, you can read and store the value by using this:
username = self.request.get('username')
At last, you can make you favorite information onto the /success page by using this simple code:
self.response.out.write('You\'ve succeeded, ' + username + '!')
But, it's of course not a safe way to pass password. I wish it helps.
Looks like there's a similar question asked here: Google App Engine self.redirect post
The answer to that one recommends using the urlfetch.fetch() to do the post.
import urllib
form_fields = {
"first_name": "Albert",
"last_name": "Johnson",
"email_address": "Albert.Johnson#example.com"
}
form_data = urllib.urlencode(form_fields)
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
result = urlfetch.fetch(url=url,
payload=form_data,
method=urlfetch.POST,
headers=headers)
Related
I am trying to connect to the api as explained in http://api.instatfootball.com/ , It is supposed to be something like the following get /[lang]/data/[action].[format]?login=[login]&pass=[pass]. I know the [lang], [action] and [format] I need to use and I also have a login and password but donĀ“t know how to access to the information inside the API.
If I write the following code:
import requests
r = requests.get('http://api.instatfootball.com/en/data/stat_params_players.json', auth=('login', 'pass'))
r.text
with the actual login and pass, I get the following output:
{"status":"error"}
This API requires authentication as parameters over an insecure connection, so be aware that this is highly lacking on the API part.
import requests
username = 'login'
password = 'password'
base_url = 'http://api.instatfootball.com/en/data/{endpoint}.json'
r = requests.get(base_url.format(endpoint='stat_params_players'), params={'login': username, 'pass': password})
data = r.json()
print(r.status_code)
print(r.text)
You will need to make a http-request using the URL. This will return the requested data in the response body. Depending on the [format] parameter, you will need to decode the data from xml / json to a native Python object.
As rdas already commented, you can use the request library for python (https://requests.readthedocs.io/en/master/). You will also find some code samples there. It will also do proper decoding of JSON data.
If you want to play around with the API a bit, you can use a tool like Postman for testing and debugging your requests. (https://www.postman.com/)
does anyone have any idea, why the output of this script, where i use requests.post to login is code 404, Not found, and the same script, where I use only requests.get has code 200 OK? What should I change?
import requests
URL = 'https://www.stratfor.com/login'
session = requests.Session()
page = session.post(URL)
print(page.status_code, page.reason)
Thank you.
it seem to be worked with get request and should returned 405 but it depends on the server
One good way to note the right page to log in is to log the network calls.
After looking at the calls, a request is sent to
URL = https://www.stratfor.com/api/v3/user/login
The API endpoint actually expects a payload like this:
payload = {username: "YOU_USER", password: "YOUR_PASS"}
Try something like this:
r = requests.post(URL,json=payload)
You might need to pass more headers, which you can poke the network call log for. Although, it seems like that user and password are passed as raw strings here? If so, that's definitely not safe.
I want to take the password from the user and then send a post request to a url ('http://example.com/welcome?user=' + (some user name)). So how can I send a post request with data?
data = {'password': SOME_PASSWORD}
requests.post(url, data=data)
your url would be something like
'http://example.com/welcome?user={}'.format(username)'
Then at you server you have the username in the get parameters and password in the post parameters. But the you could just put the user and the password in the data dictionary and post both fields
EDIT:
I do agree with Martijn that passing some data in get and some in post is a bad idea, so if there is not a good reason to do it you probably should avoid it.
I also like the idea of using the requests params argument and data arguments if you absolutely need to pass them the username in GET and the password in POST, so your request would become:
requests.post('http://example.com/welcome', data={'password': SOME_PASSWORD}, params={'user': SOME_USERNAME})
but the best way is:
requests.post('http://example.com/welcome', data={'password': SOME_PASSWORD, 'user': SOME_USERNAME})
Apologies for the short answer previously, it was a quick one in a coffee break. Please forgive me :-)
In Flask, I want to send an URL in the URL as a parameter with all its parameters
Here is a working example
http://graph.facebook.com/https://www.facebook.com/photo.php?fbid=717097101644637&set=a.456449604376056.98921.367116489976035&type=1&theater
I couldn't find a way, because the moment I cal something like this, the url doesn't go true.
#app.route('/<url>', methods=['GET'])
def show(url):
""" do something with it """
Any idea how to do it?
Thank you
If you look at what Facebook returns for that URL:
{
"id": "https://www.facebook.com/photo.php",
"shares": 377224,
"comments": 2
}
you will note that the query string params are being consumed by graph.facebook.com - they are not treated as part of the URL that is being submitted as a search param. If you want to be able to get the whole URL including query string arguments, you will need to URL-encode the query string your.url/https://www.facebook.com/photo.php%3Ffbid=717097101644637%26set=a.456449604376056.98921.367116489976035%26type=1%26theater
Unfortunately, I can't control if the URL is URL-encoded so the best thing I came along is this.
_url = request.url.replace(request.url_root,'')
url = urllib.unquote(_url)
Maybe it will help somebody.
I have a url like this:
http://site/account/login/
I want to do this through command-line, without form.
import requests
requests.get(url, auth=('username-goes-here', 'password-goes-here'))
But how do I design my views function in Pyrmaid using Cornice?
#user.get()
def login(request):
I don't see the auth tuple appears in requests.GET at all. I need to be able to get those parameters out...
Any idea?
You can use HTTP headers to pass authentication token.
See http://cornice.readthedocs.org/en/latest/tutorial.html