API Post Python - python

I am trying to make a post request within the Matchbook API.
I have logged in and I got below "Session- Tocken":
{"session-token":"xxxx_b0b8a6f22a82396b6afcfa344f3022","user-id":xx685,"role":"USER"}
However, I am not sure how to make the post request. See below code used:
headers = {"session-token" : "xxxx_b0b8a6f22a82396b6afcfa344f3022"}
r = requests.post('https://api.matchbook.com/edge/rest/reports/v1/offers/current/?odds-type=DECIMAL&exchange-type=binary&currency=EUR, headers = headers')
print r.text
Below is the error message that I got. It does not make sense to me because I logged in successfully and got the above session-token in response.
{"errors":[{"messages":["You are not authorised to access this resource. Login to continue."]}]}
Am I properly indicating the session-token in the header information of the post request?

You need to pass headers argument in post function.
headers = {"session-token" : "xxxx_b0b8a6f22a82396b6afcfa344f3022"}
response = requests.post('https://api.matchbook.com/edge/rest/reports/v1/offers/current/?odds-type=DECIMAL&exchange-type=binary&currency=EUR', headers=headers)
also if you need to get an json response, just call json() function on response variable.
something like response.json()

Related

Python POST requests return with "Response 400"

I am trying to login on a website by use method requests POST.
ssn = requests.session()
data = {"LoginForm[email]":"gmail#gmail.com", "LoginForm[password]":"pw12345", "LoginForm[rememberMe]":0}
r = ssn.post("https://www.fshare.vn/site/login", data = data)
print(r.json)
# bound method Response.json of <Response [400]>
as I know, I missing a value in data is called "csrf-app" :
HTTP Header POST : _csrf-app=gXvuYHZnQpoA37zC1Yncpb1BMstVUqRcVdj9x1QHJqK1AoYoHlUV21G9jaya0Jjy9iJRrz1_nRIft4ywO0RD-g==&LoginForm[email]=gmail#gmail.com&LoginForm[password]=pw12345&LoginForm[rememberMe]=0
So how to get this value ""csrf-app" to post that along the other data ?
CSRF is used to avoid someone calling the APIs without the form.
You can refer to this post for more information. You should instead look for their login REST APIs, and see if they have any.

Python API Post Request URL Returning Error

I'm trying to use this service, Buffer, to publish a post on Facebook through API.
Here's my code:
params = {
'profile_ids': ids,
'text': "text",
'access_token': access_token
}
r = requests.post('https://api.bufferapp.com/1/updates/create.json', params=params)
print r.json()
print(r.url)
But when run I it, it prints out this message:
{"success":false,"message":"Please select at least one account to post from.","code":1004}
This is the URL used to make the request:
https://api.bufferapp.com/1/updates/create.json?access_token=ACCESS_TOKEN&text=TEXT&profile_ids=LIST_WITH_PROFILE_IDS
I made sure the id is correct and I got it both manually and through API still, I get the same message.
The only required parameter is the profile_ids which should be an array. Here's the documentation on their website about publishing posts("updates"): https://buffer.com/developers/api/updates
What am I doing wrong here?
It seems that the API expects the data in the post request payload, not as url parameters.
Use data, not params.
r = requests.post('https://api.bufferapp.com/1/updates/create.json', data=params)
# -------------------------------------------------------------------^^^^ here

Trouble getting data from REST http service using requests package

This works fine, I can get data returned:
r = urllib2.Request("http://myServer.com:12345/myAction")
data = json.dumps(q) #q is a python dict
r.add_data(data)
r=urllib2.urlopen(r)
But doing the same with requests package fails:
r=requests.get("http://myServer.com:12345/myAction", data=q)
r.text #This will return a message that says method is not allowed.
It works if I make it a post request: r=requests.post("http://myServer.com:12345/myAction", data=json.dumps(q))
But why?
According to the urllib2.urlopen documentation:
the HTTP request will be a POST instead of a GET when the data parameter is provided.
This way, r=urllib2.urlopen(r) is also making a POST request. That is why your requests.get does not work, but requests.post does.
Set up a session
import session
session = requests.Session()
r = session.get("http://myServer.com:12345/myAction", data=q)
print r.content (<- or could us r.raw)

slack api calls through python request library

I was making slack api calls through python library slackclient which is a wrapper around slack api. However, for some cases I need to make conventional api calls also with url and get/post method. I was trying to open a direct message channel with another user by my bot. The documentation - https://api.slack.com/methods/im.open says to "Present these parameters as part of an application/x-www-form-urlencoded querystring or POST body. application/json is not currently accepted."
Now in python, I can write,
url = 'https://slack.com/api/im.open'
headers = {'content-type':'x-www-form-urlencoded'}
data = {'token':BOT_TOKEN, 'user':user_id, 'include_locale':'true','return_im':'true'}
r= requests.post(url,headers,data )
print r.text
The message I get is {"ok":false,"error":"not_authed"}
I know the message is "not authed" although I use my bot token and another user id, my hunch is that I'm sending the request in wrong format because I just wrote it some way reading the documentation. I'm not sure how to exactly send these requests.
Any help?
since the Content-Type header is x-www-form-urlencoded sending data in form of dictionary does not work. you can try something like this.
import requests
url = 'https://slack.com/api/im.open'
headers = {'content-type': 'x-www-form-urlencoded'}
data = [
('token', BOT_TOKEN),
('user', user_id),
('include_locale', 'true'),
('return_im', 'true')
]
r = requests.post(url, data, **headers)
print r.text
The second parameter in requests.post is used for data, so in your request you're actually posting the headers dictionary. If you want to use headers you can pass arguments by name.
r= requests.post(url, data, headers=headers)
However this is not necessary in this case because 'x-www-form-urlencoded' is the default when posting form data.

Making a successful Python HTTP POST Request

I am trying to write a python script that will make a request to a desktop application listening to 8080 port. The below is the code that I use to make the request.
import requests
payload = {"url":"abcdefghiklmnopqrstuvwxyz=",
"password":"qertyuioplkjhgfdsazxvnm=",
"token":"abcdefghijklmn1254786=="}
headers = {'Content-Type':'application/json'}
r = requests.post('http://localhost:9015/login',params = payload, headers=headers)
response = requests.get("http://localhost:9015/login")
print(r.status_code)
After making the request, I get a response code of 401.
However, when I try the same using the Postman app, I get a successful response. The following are the details I give in Postman:
URL: http://localhost:9015/login
METHOD : POST
Headers: Content-Type:application/json
Body: {"url":"abcdefghiklmnopqrstuvwxyz=",
"password":"qertyuioplkjhgfdsazxvnm=",
"token":"abcdefghijklmn1254786=="}
Can I get some suggestions on where I am going wrong with my python script?
You pass params, when you should pass data, or, even better, json for setting Content-Type automatically. So, it should be:
import json
r = requests.post('http://localhost:9015/login', data=json.dumps(payload), headers=headers)
or
r = requests.post('http://localhost:9015/login', json=payload)
(params adds key-value pairs to query parameters in the url)

Categories

Resources