I'm using requests to make a POST request to GroupMe's image service that should return a URL of the hosted image that I can use to post to a GroupMe thread. The documentation mentions that I need my access token and the binary image data in the payload in order to do this.
Here is a very simple example of how my code to do this currently looks:
import requests
access_token = 'my_access_token'
img_path = 'picture_name.jpg'
img_service_url = 'https://image.groupme.com/pictures'
r = requests.post(img_service_url, files={'file': img_path})
EDIT:
I looked at the documentation and source for the groupy.api.endpoint module Groupy(https://groupy.readthedocs.io/en/v0.6.2/_modules/groupy/api/endpoint.html#Images) and updated my script (reflected above) to use the same requests function parameters, but to no avail. Now the code returns a 500.
This worked for me (avatar.jpeg is in the same folder as my testing.py code below)
# curl 'https://image.groupme.com/pictures'
# -X POST
# -H "X-Access-Token: $GM_TOKEN"
# -H "Content-Type: image/jpeg"
# --data-binary #AwesomePicture.jpg
import requests
data = open('./avatar.jpeg', 'rb').read()
res = requests.post(url='https://image.groupme.com/pictures',
data=data,
headers={'Content-Type': 'image/jpeg',
'X-Access-Token': 'ACCESS_TOKEN'})
print(res.content)
OUTPUT
b'{"payload":{"url":"https://i.groupme.com/100x100.jpeg.5b71f15633f6454ca6a3a6b3e267a3fb","picture_url":"https://i.groupme.com/100x100.jpeg.5b71f15633f6454ca6a3a6b3e267a3fb"}}\n'
Related
I'm trying to request to my flask webserver with an image using python and just can't get it to work.
Using cURL it's simple:
curl -XPOST -F "file=#image.jpg" http://127.0.0.1:5001
But in python using my code:
import requests
with open("image.jpg", "rb") as a_file:
file_dict = {"image.jpg": a_file}
response = requests.post("http://127.0.0.1:5001", files=file_dict)
print(response.text)
print(response.status_code)
I simply get the HTML of the site returned and status 200. Not the JSON that returns using cURL (and is what I want returned).
Any help would be appreciated, Thanks.
you can use follow code
files = {'file': open('image.jpg', 'rb')}
r = requests.post('http://127.0.0.1:5001', files=files)
print(r.text)
So I'm using FreshDesk API and able to get the response using request module, But whenever I'm using proxy servers I'm unable to get the response.
import base64
import requests
import os
from requests.auth import HTTPBasicAuth
import ssl
method = "get"
url = "https://mycompanydomain.freshdesk.com/api/v2/tickets"
apiKey = "XXXXXXXXX"
secret = "x"
os.environ["REQUESTS_CA_BUNDLE"] = "Path to CA Certs"
auth = HTTPBasicAuth(apiKey, secret)
rsp = requests.request(method, url, headers=None, auth=auth)
print(rsp.text)
But whenever I'm using the proxy server in my organization, I'm getting an error message as {"code":"invalid_credentials","message":"You have to be logged in to perform this action."}
Code which I'm using for the proxy servers
import base64
import requests
import http.client
import urllib.parse
method = "get"
apiKey = "XXXXXXXX"
secret = "x"
url = "https://mycompanydomain.freshdesk.com/api/v2/tickets"
cred= '{}:{}'.format(apiKey, secret)
cred = base64.b64encode(cred.encode('utf-8')).decode('utf-8')
authorization_headers = {
'Proxy-Authorization': 'Basic {}'.format(cred)
}
conn = http.client.HTTPSConnection("11.125.250.121", 3128)
conn.set_tunnel("mycompanydomain.freshdesk.com", headers = authorization_headers)
headers = { 'Content-Type' : 'application/json' }
conn.request("GET", "/api/v2/tickets",headers = headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
FreshDesk API Docs for using their API
curl -v -u abcdefghij1234567890:X -H "Content-Type: application/json" -X GET 'https://domain.freshdesk.com/api/v2/tickets'
Any possible way to resolve this error?
Here is a bit of a guess, I haven't actually tried it but I do use Freshdesk, and I should be using their API.
Here is a link to the Freshdesk API:
https://support.freshdesk.com/support/solutions/articles/216548-create-and-update-tickets-with-custom-fields-using-api
I would try to taking out the "-H "Content-Type: application/json" to better match the suggested code. I would add the Content Type for POSTS not GETs in most cases, unless the API specifically calls for it. Try it and let us know how it works.
curl -u API_KEY:X -X GET https://domain.freshdesk.com/api/v2/ticket_fields
Encoding the api key with the x for example as follows: someapikey:x helps.
See link:
How do I encode and decode a base64 string?
Also see FreshDesk api doc:
https://developers.freshdesk.com/api/#authentication
See the Note which says Encode if plain api key does not work.
Finding it problematic to convert a simple CURL request in Windows to a Python script
The CURL command is
curl -X POST -d "{\"query\": \"NEW YORK\"}" http://192.168.0.106:8080/parser
I get the output:
[{"label":"state","value":"new york"}]
The Python script is
from urllib.parse import urlencode
from urllib.request import Request, urlopen
url = 'http://192.168.0.106:8080/parser' # Set destination URL here
post_fields = {"query": "NEW YORK"} # Set POST fields here
request = Request(url, urlencode(post_fields).encode())
json = urlopen(request).read().decode()
print(json)
The output is []. Nothing basically.
Just use requests:
import requests
data = '{"query": "NEW YORK"}'
response = requests.post('http://192.168.0.106:8080/parser', data=data)
Full documentation can be found at Requests: HTTP for Humans
I am trying to access data using API which is behind the proxy server. If I use curl command like below it works:
curl --proxy http://MY_PROXY_SERVER:PORT --header "Accept: application/csv" http://WEB_SERVER_ADDRESS/data/CHANNEL?start=1470011400
I get the data which is expected.
When I am trying to access the same URL with python either by requests or urllib2 I am not able to get the data back. This is the code:
from __future__ import print_function
import requests
s = requests.Session()
s.proxies = {"http": "http://MY_PROXY_SERVER:PORT"}
headers = {'Accept': 'application/csv'}
url = "http://WEB_SERVER_ADDRESS/data/CHANNEL?start=1470011400"
r = s.get(url, headers=headers)
print(r.text)
I don't get any error and request is able to go through the python successfully. The output is empty list. I also tried with other media type supported by API like 'json', issue still persists.
I'm new to web services and am trying to send the following JSON based request using a python script:
http://myserver/emoncms2/api/post?apikey=xxxxxxxxxxxxx&json={power:290.4,temperature:19.4}
If I paste the above into a browser, it works as expected. However, I am struggling to send the request from Python. The following is what I am trying:
import json
import urllib2
data = {'temperature':'24.3'}
data_json = json.dumps(data)
host = "http://myserver/emoncms2/api/post"
req = urllib2.Request(host, 'GET', data_json, {'content-type': 'application/json'})
response_stream = urllib2.urlopen(req)
json_response = response_stream.read()
How do I add the apikey data into the request?
Thank you!
Instead of using urllib2, you can use requests. This new python lib is really well written and it's easier and more intuitive to use.
To send your json data you can use something like the following code:
import json
import requests
data = {'temperature':'24.3'}
data_json = json.dumps(data)
payload = {'json_payload': data_json, 'apikey': 'YOUR_API_KEY_HERE'}
r = requests.get('http://myserver/emoncms2/api/post', data=payload)
You can then inspect r to obtain an http status code, content, etc
Even though this doesnt exactly answer OPs question, it should be mentioned here that requests module has a json option that can be used like this:
import requests
requests.post(
'http://myserver/emoncms2/api/post?apikey=xxxxxxxxxxxxx',
json={"temperature": "24.3"}
)
which would be equivalent to the curl:
curl 'http://myserver/emoncms2/api/post?apikey=xxxxxxxxxxxxx' \
-H 'Content-Type: application/json' \
--data-binary '{"temperature":"24.3"}'
Maybe the problem is that json.dumps puts " and in the json you put in the url there are no "s.
For example:
data = {'temperature':'24.3'}
print json.dumps(data)
prints:
{"temperature": "24.3"}
and not:
{temperature: 24.3}
like you put in your url.
One way of solving this (which is trouble prone) is to do:
json.dumps(data).replace('"', '')