Python - Using requests sends post message - python
I used Postman to send a raw request to Jetstar website to get the flight details. And I wanted to use python script to do the same thing using requests library, but I cannot get back the correct response.
Here what I have done in Postman:
And a simple script I used to send post request:
import requests
files = {'file': open('PostContent.txt', 'rb')}
if __name__ == "__name__"):
url = "http://www.jetstar.com/"
r = requests.post(url, files = files)
print(r.text)
When I run the python script, I always get the welcome page not flight details. I am not sure what is error?
Note: The PostContent.txt contains the form-data in raw text when I search for flights.
I used Chrome Dev Tool to capture the POST request when I search for a specific flight date. And it is the Form Data in the Headers.
Try using a dictionary instead of a FILE. The FILE is supposed to be for posting a FILE, not a FORM-ENCODED post, which is probably what the site expects.
payload = {
'DropDownListCurrency': 'SGD'
}
r = requests.post("http://httpbin.org/post", data=payload)
You use a key file which is wrong for this type of request. Also your sample code isn't working! Just paste working code here...
import requests
import logging
logging.basicConfig(level=logging.DEBUG)
payload = {"__EVENTTARGET":"",
"__EVENTARGUMENT":"",
"__VIEWSTATE":"/wEPDwUBMGQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgEFJ01lbWJlckxvZ2luU2VhcmNoVmlldyRtZW1iZXJfUmVtZW1iZXJtZSDCMtVG/1lYc7dy4fVekQjBMvD5",
"pageToken":"",
"total_price":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$RadioButtonMarketStructure":"RoundTrip",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketOrigin1":"Nadi (NAN)",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketDestination1":"Melbourne (Tullamarine) (MEL)",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureDate1":"14/01/2015",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDestinationDate1":"16/02/2015",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListCurrency":"AUD",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketOrigin2":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketDestination2":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureDate2":"16/02/2015",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDestinationDate2":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketOrigin3":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketDestination3":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureDate3":"27/12/2014",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDestinationDate3":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketOrigin4":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketDestination4":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureDate4":"03/01/2015",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDestinationDate4":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketOrigin5":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketDestination5":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureDate5":"10/01/2015",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDestinationDate5":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketOrigin6":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketDestination6":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureDate6":"17/01/2015",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDestinationDate6":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListPassengerType_ADT":1,
"ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListPassengerType_CHD":0,
"ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListPassengerType_INFANT":0,
"ControlGroupSearchView$AvailabilitySearchInputSearchView$RadioButtonSearchBy":"SearchStandard",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMultiCityOrigin1":"Origin",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMultiCityDestination1":"Destination",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureMultiDate1":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMultiCityOrigin2":"Origin",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMultiCityDestination2":"Destination",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureMultiDate2":"",
"ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMultiPassengerType_ADT":1,
"ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMultiPassengerType_CHD":0,
"ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMultiPassengerType_INFANT":0,
"ControlGroupSearchView$AvailabilitySearchInputSearchView$numberTrips":2,
"ControlGroupSearchView$AvailabilitySearchInputSearchView$ButtonSubmit":""}
if __name__ == "__main__":
url = "http://booknow.jetstar.com/Search.aspx"
r = requests.post(url, data=payload)
print(r.text)
Related
Mediawiki api returning http response code instead of json
I am following some example code in the mediawiki api (see link at the bottom) to get a response in json format that looks something like the following: "login": { "lguserid": 21, "result": "Success", "lgusername": "William" } Unfortunately, I am not getting anything similar to that and instead just get a response that looks as follows: <Response [200]> Below is the example code that I'm almost using verbatim at this point, to no avail. #!/usr/bin/python3 """ login.py MediaWiki API Demos Demo of `Login` module: Sending post request to login MIT license """ import requests USERNAME = "my_bot_username" PASSWORD = "my_bot_password" S = requests.Session() URL = "https://www.mediawiki.org/w/api.php" # Retrieve login token first PARAMS_0 = { 'action':"query", 'meta':"tokens", 'type':"login", 'format':"json" } R = S.get(url=URL, params=PARAMS_0) Again, R is supposed to be in json format instead it is just a http response code not in json format which causes the very next line of the example script (see link at the bottom) fail. Is the example code outdated or can anyone spot why the code above is not returning a response in json format? EDIT: The version of mediawiki I am using is 1.17.0 https://www.mediawiki.org/wiki/API:Login#Python
You have to use response.json() to get a json object : R = S.get(url=URL, params=PARAMS_0) DATA = R.json()
How to send a file using POST method to TelegramBot?
I want to send a file with the post method, but I don't know what's wrong with my code I have chat_id, file_id, and every requirement parameters this is a sample code for sending voice via POST Request import requests my_data = {'chat_id': '72600457' ,'file_id': 'AwADBAADPAYAAvFWCVFZFfPyZdGLfhYE'} my_url = 'https://api.telegram.org/bot<MY TOKEN>/sendVoice' request.post(url=my_url, data=my_data) when I run the code, no error happens. But nothing is shown from the bot; This file_id works with GET METHOD and I could send text with POST METHOD, But for files it seems it doesn't work.
Check documentation for sendVoice - it doesn't use name file_id but voice data = {'chat_id': '72600457', 'voice': 'AwADBAADPAYAAvFWCVFZFfPyZdGLfhYE'} If you use file ID then you can use POST but also GET And you should get response from server to see information about wrong request import requests token = '<MY TOKEN>' data = {'chat_id': '72600457', 'voice': 'AwADBAADPAYAAvFWCVFZFfPyZdGLfhYE'} url = f'https://api.telegram.org/bot{token}/sendVoice' #response = requests.post(url, data=data) response = requests.get(url, params=data) print(response.json()) By the way: there is module python-telegram-bot. GitHub: python-telegram-bot
How to send json from Rails
From Python I make request to Rails app, and get this response: {u'answer': u'ok'} and in Rails side code like that: #res = {'answer'=> 'ok'} render json: #res May this problem on python side? Here code of python: import requests import json URL1 = 'http://' PARAMS = {'login': 'asd'} r = requests.get(url = URL1, params = PARAMS) data = r.json() print(data) How I can get clear json
You can try as data = r.as_json
You are specifically parsing the JSON contained in the response body into Python objects using r.json(). Use r.text to see the textual representation of the response body.
In a Rails controller you can do: render :json => data.to_json and return In the CLI you'd do: puts data.to_json Make sure to include json or a compatible gem in your CLI code.
Requests.Get not working for API call - Yielding 401 Error and "Not Logged In" - Second Call
I have successfully connected to the target server using requests.post; however, when I try to make any request.get calls I am receiving the following error: {"message":"Not logged in","code":401} I have confirmed I am logging in successfully, as when I print(response.text) I am showing info from the server. Based on the error message, it appears that I am not persisting the login. My script: import requests data = {} payload = {'j_username': 'my_username', 'j_password': 'my_password'} response = requests.post('https://sample.com', data=payload) getBlocks_response = requests.get('https://sample.com/api/v1/resourceBlock') print(getBlocks_response.text) getDataDateRange_response = requests.get('https://sample.com/api/v1/factData/getDataDateRange', data=payload) print(getDataDateRange_response.text) I am expecting an output of data in the form of a JSON, but have only received two errors for both requests.get calls above. I am new to Python as well as API interaction. Any insight would be greatly appreciated. Thanks.
I read the documentation regarding Session(). I was able to get my get requests to run successfully by adjusting my script to: import requests with requests.Session() as session: data = {} payload = {'j_username': 'my_username', 'j_password': 'my_password'} response = session.post('https://sample.com', data=payload) getBlocks_response = session.get('https://sample.com/api/v1/resourceBlock') print(getBlocks_response.text) getDataDateRange_response = session.get('https://sample.com/api/v1/factData/getDataDateRange', data=payload) print(getDataDateRange_response.text)
How to call an API using Python Requests library
I can't figure out how to call this api correctly using python urllib or requests. Let me give you the code I have now: import requests url = "http://api.cortical.io:80/rest/expressions/similar_terms?retina_name=en_associative&start_index=0&max_results=1&sparsity=1.0&get_fingerprint=false" params = {"positions":[0,6,7,29]} headers = { "api-key" : key, "Content-Type" : "application/json"} # Make a get request with the parameters. response = requests.get(url, params=params, headers=headers) # Print the content of the response print(response.content) I've even added in the rest of the parameters to the params variable: url = 'http://api.cortical.io:80/rest/expressions/similar_terms?' params = { "retina_name":"en_associative", "start_index":0, "max_results":1, "sparsity":1.0, "get_fingerprint":False, "positions":[0,6,7,29]} I get this message back: An internal server error has been logged # Sun Apr 01 00:03:02 UTC 2018 So I'm not sure what I'm doing wrong. You can test out their api here, but even with testing I can't figure it out. If I go out to http://api.cortical.io/, click on the Expression tab, click on the POST /expressions/similar_terms option then paste {"positions":[0,6,7,29]} in the body textbox and hit the button, it'll give you a valid response, so nothing is wrong with their API. I don't know what I'm doing wrong. can you help me?
The problem is that you're mixing query string parameters and post data in your params dictionary. Instead, you should use the params parameter for your query string data, and the json parameter (since the content type is json) for your post body data. When using the json parameter, the Content-Type header is set to 'application/json' by default. Also, when the response is json you can use the .json() method to get a dictionary. An example, import requests url = 'http://api.cortical.io:80/rest/expressions/similar_terms?' params = { "retina_name":"en_associative", "start_index":0, "max_results":1, "sparsity":1.0, "get_fingerprint":False } data = {"positions":[0,6,7,29]} r = requests.post(url, params=params, json=data) print(r.status_code) print(r.json()) 200 [{'term': 'headphones', 'df': 8.991197733061748e-05, 'score': 4.0, 'pos_types': ['NOUN'], 'fingerprint': {'positions': []}}]
So, I can't speak to why there's a server error in a third-party API, but I followed your suggestion to try using the API UI directly, and noticed you're using a totally different endpoint than the one you're trying to call in your code. In your code you GET from http://api.cortical.io:80/rest/expressions/similar_terms but in the UI you POST to http://api.cortical.io/rest/expressions/similar_terms/bulk. It's apples and oranges. Calling the endpoint you mention in the UI call works for me, using the following variation on your code, which requires using requests.post, and as was also pointed out by t.m. adam, the json parameter for the payload, which also needs to be wrapped in a list: import requests url = "http://api.cortical.io/rest/expressions/similar_terms/bulk?retina_name=en_associative&start_index=0&max_results=1&sparsity=1.0&get_fingerprint=false" params = [{"positions":[0,6,7,29]}] headers = { "api-key" : key, "Content-Type" : "application/json"} # Make a get request with the parameters. response = requests.post(url, json=params, headers=headers) # Print the content of the response print(response.content) Gives: b'[[{"term":"headphones","df":8.991197733061748E-5,"score":4.0,"pos_types":["NOUN"],"fingerprint":{"positions":[]}}]]'