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 :-)
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 am using am attempting to do a bulk download of a series of PDFs from a site that requires login authentication. I am able to successfully log in, however, when I attempt a GET request for '/transcripts/transcript.pdf?user_id=3007' but, the request returns the content for '/transcripts/transcript.pdf'.
Does anyone have any idea why the URL param is not sending? Or why it would be rerouted?
I have tried passing the parameter 'user_id' as data, params, and hardcoded in the URL.
I have removed the actual domain from the strings below just for privacy
with requests.Session() as s:
login = s.get('<domain>/login/canvas')
# print the html returned or something more intelligent to see if it's a successful login page.
print(login.text)
login_html = lxml.html.fromstring(login.text)
hidden_inputs = login_html.xpath(r'//form//input[#type="hidden"]')
form = {x.attrib["name"]: x.attrib["value"] for x in hidden_inputs}
print("form: ",form)
form['pseudonym_session[unique_id]']= username
form['pseudonym_session[password]']= password
response = s.post('<domain>/login/canvas',data=form)
print(response.url, response.status_code) # gets <domain>?login_success=1 200
# An authorised request.
data = { 'user_id':'3007'}
r = s.get('<domain>/transcripts/transcript.pdf?user_id=3007', data=data)
print(r.url) # gets <domain>/transcripts/transcript.pdf
print(r.status_code) # gets 200
with open('test.pdf', 'wb') as f:
f.write(r.content)
GET response returns /transcripts/transcript.pdf and not /transcripts/transcript.pdf?user_id=3007
From the looks of it, you are trying to use canvas. I'm pretty sure in canvas, you can bulk download all test attachments.
If that's not the case, There are a few things to try:
after logging in, try typing the url with user_id into a browser. Does that take you directly to the PDF file or links to one?
if so, look at the url, it may simply not display the parameters; some websites do this, don't worry about it
If not, GET may not be enough; perhaps the site uses javascript, etc.
after looking through the '.history' of the request I found a series of 302 redirects.
The first was to '/login?force_login=0&target_uri=%2Ftranscripts%2Ftranscript.pdf'
In a desperate attempt, I tried: s.get('/login?force_login=0&target_uri=%2Ftranscripts%2Ftranscript.pdf%3Fuser_id%3D3007') and this still rerouted me a few times but ultimately got me the file I wanted!
If anyone has a more elegant solution to this or any resources that I can read I would greatly appreciate it!
I have set up everything required for the foursquare push api, including a secure server.
Now my question is what do I need to do to get data from that request and display on my web page. Foursquare API sends a POST to a URL which I can handle with a view.
Do I need to use AJAX calls, or just handle the post request in a view and update something in the database and show data from that database with ajax.. open to suggestions.
No, all you need to do is send requests and receive responses. Please take a look at this -> python-requests.org -> The documentation provided how you can send and receive requests.
So, for example, you could send a POST request like so:
r = requests.get('https://api.github.com/user', <your arguments here>)
And now r would contain the POST response from the said url.
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)