I am used request in python. I try to sent a post request and need send array of array. This is my code:
import requests
def connecttomlapipost(url, vector):
headers = {'content-type': 'application/json'}
r= requests.post(url, verify=False, auth=('admin', 'admin'), data = vector, headers=headers)
return r
print connecttomlapipost('https://......', [[2,3],[1,5]])
the server return: error 500. I tested using POSTMAN and the server return a correct result
You're using post and are most likely interested in using data instead or params. It should be a dictionary structure instead of a list though. See the docs.
Related
i'm trying to send a post request to my express server using python, this is my code:
import requests
print("started");
URL = "http://localhost:5000/api/chat"
PARAMS = {'ID':"99","otherID":"87",'chat':[{"senderName":"tom","text":"helloworld"}]}
r = requests.post(url = URL, data = PARAMS)
pastebin_url = r.text
print("The pastebin URL is:%s"%pastebin_url)
but when I recieve the call, I get an empty object on my Node server, am I missing something?
(With postman it works fine so its not the server)
typically requests library differentiates on the type of request depending on the param used to make the request. Meaning if you intend to make a JSON post then one should use the json param like so:
response = requests.post(url=URL, json=PARAMS)
what this does is set the accompanying headers which is why when your express server attempts to parse it, it comes back empty
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¤cy=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¤cy=EUR', headers=headers)
also if you need to get an json response, just call json() function on response variable.
something like response.json()
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.
I am trying to send a simple POST request to a server using requests. I am doing (I think at least) exactly what the quickstart (http://docs.python-requests.org/en/master/user/quickstart/) is saying to do. The POST request seems to be ignoring the data= tag and not appending the data to the end of the url. This is what I have:
import requests, json
url = 'http://localhost:5000/todo/api/v1.0/tasks'
payload = (('key1', 'value1'), ('key1', 'value2'))
r=requests.post(url, data=payload)
print 'url is: ', r.url
and the output is:
url is: http://localhost:5000/todo/api/v1.0/tasks
I don't know if it is relevant or not, but if I use the GET tag params=, the url is assembled as I expect:
r=requests.post(url, params=payload)
url is: http://localhost:5000/todo/api/v1.0/tasks/?key1=value&key1=value2
Anyone see anything wrong? Thanks in advance
Try to dump payload to json and use a dict
payload = {}
payload[key1] = value1
payload[key2] = value2
payload_data = json.dumps(payload)
r=requests.post(url, data=payload_data)
POST data is passed in the body of the request, not the URL. A POST HTTP request looks something like this
POST /login HTTP/1.1
Host: example.com
Content-Length: <length>
firstname=john&lastname=doe
So as you can see the URL does not get passed these parameters and this is important for a lot of reasons that I won't mention in this answer. However, if for some reason you do need to pass these parameter in the URL instead of the body of the request then here is oh you might do it.
import requests, urllib
url = 'http://localhost:5000/todo/api/v1.0/tasks'
payload = (('key1', 'value1'), ('key1', 'value2'))
request_data = urllib.urlencode(payload) # Turns it into key1=value1&key1=value2
response = requests.post(url + "?" + request_data)
That will craft the request in the same way that you see it in the GET request that you have at the bottom of your answer, but instead as a 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)