I have a quick question to call an NLTK Api using python.To find the sentiment of "great";
The API syntax is
$ curl -d "text=great" http://text-processing.com/api/sentiment/
I need to use a python request to post this and to receive a json object as response.I am trying to use with
resp = requests.post(url, data=values, allow_redirects=True)
if url is http://text-processing.com/api/sentiment/
how must text parameter to be passed?
Modifying the guide from the requests documentation, to suit your requirement, this is what you do:
>>> import json
>>> url = 'http://text-processing.com/api/sentiment/'
>>> payload = {'text': 'great'}
>>> r = requests.post(url, data=json.dumps(payload))
Related
So i was following the guide for getting a OAUTH token from https://developer.spotify.com/documentation/general/guides/authorization/client-credentials/
I tried doing a python requests library of the above equivalent code but i got response 400 from the server. May I know where i am going wrong?
import requests
import json
import clientidandsecret
headers ={"Authorization": "Basic " + clientidandsecret.C_ID +":" +clientidandsecret.C_SECRET}
form = {"form":{"grant_type" :"client_credentials"}, "json":"true"}
result = requests.post("https://accounts.spotify.com/api/token", headers=headers, data=form)
print(result)
Your variable form is a Dict, if you want to use the parameter data in requests it needs to be a string, this will fix it:
import json
...
result = requests.post(url, headers=headers, data=json.dumps(form))
Or even better:
result = requests.post(url, headers=headers, json=form)
I am trying to recreate a Curl command from the Gentle forced aligner:
https://github.com/lowerquality/gentle
Curl command:
curl -F "audio=#audio.mp3" -F "transcript=#words.txt" "http://localhost:8765/transcriptions?async=false"
Code so far:
import requests
url = 'http://localhost:8765/transcriptions/'
files = {'audio': open('C://Users//user//Desktop//gentle//audio.mp3','rb'), 'transcript':
open('C://Users//andrey_user//Desktop//gentle//words.txt', 'rb')}
headers = {}
response = requests.post(url,
files=files,
headers=headers)
print(response.text)
But it only returns the HTML and says it is a GET request, however the curl command gives me the correct result. Thanks for your help!
Try this code :
import requests
params = (
('async', 'false'),
)
files = {
'audio': ('audio.mp3', open('audio.mp3', 'rb')),
'transcript': ('words.txt', open('words.txt', 'rb')),
}
response = requests.post('http://localhost:8765/transcriptions', params=params, files=files)
print(response.text)
I believe you also have to convert the response to JSON, assuming that the library you're trying to send the request to is a REST API. I'm not sure why it says its a get request, but if that's the case, just try changing it to a get request and see if it works out.
So adding r1=response.json() should give you access to the correct JSON data
I am having trouble getting the requests library for python to work with ISO datetime parameters
headers = {"Authorization: Token [token_here]"}
body = {"start_date": "2019-07-01T05:00:00Z","end_date": "2019-07-02T00:00:00Z"}
resp = requests.get("https://website.com/api/v1/endpoint", headers=headers, params=body)
The above will turn the url into the following
https://website.com/api/v1/endpoint?%7B%22start_date%22:%20%222019-07-01T05:00:00Z%22,%20%22end_date%22:%20%222019-07-03T00:00:00Z%22%7D'
which results in a 405 for me. However, using the exact same body in body.json for httpie works fine
cat body.json
{"start_date": "2019-07-01T05:00:00Z","end_date": "2019-07-02T00:00:00Z"}
http https://website.com/api/v1/endpoint 'Authorization: Token [token_here]' < body.json
JSON should be sent as the body of a POST request. For that you can use the convenient json parameter of the requests.post method:
resp = requests.post("https://website.com/api/v1/endpoint", headers=headers, json=body)
I am trying to write a Python script that will allow me to accomplish what I normally would by using CURL to perform an API "GET". I have browsed through some questions on here but I am still a bit confused on how to do this; from what I have seen, I can use the "requests" library like this:
URL = requests.get("www.example.com/123")
However, the Curl command I normally run uses authentication as well. Here is an example:
curl -X GET -H 'Authorization: hibnn:11111:77788777YT666:CAL1' 'http://api.example.com/v1.11/user?id=123456
Can I still use the requests library when creating a script to pull this data? Specifically, I would need to be able to pass along the authentication info along with the URL itself.
Update:
Here is what my code looks like:
import requests
import json
url = ("api.example.com/v1.11/user?id=123456")
header = {"Authorization": "hibnn:11111:77788777YT666:CAL1"}
response = requests.get(url, headers=header)
print(response.json)
However, I am getting a response [401] error. I think I am probably doing something wrong with my headers, can anyone help?
You may pass headers as keyword argument to the get method.
>>> url = 'https://api.github.com/some/endpoint'
>>> headers = {'user-agent': 'my-app/0.0.1'}
>>> r = requests.get(url, headers=headers)
Reference
I'm trying to use the Twitch API in a Django [python] web application. I want to send a request and get information back, but I don't really know what I'm doing.
curl -H 'Accept: application/vnd.twitchtv.v2+json' -X GET \
https://api.twitch.tv/kraken/streams/test_channel
How do I convert this python?
Thanks
Using the builtin urllib2:
>>> import urllib2
>>> req = urllib2.Request('https://api.twitch.tv/kraken/streams/test_channel')
>>> req.add_header('Accept', 'application/vnd.twitchtv.v2+json')
>>> resp = urllib2.urlopen(req)
>>> content = resp.read()
If you're using Python 3.x, the module is called urllib.request, but otherwise you can do everything the same.
You could also use a third-party library for HTTP, like requests, which has a simpler API:
>>> import requests
>>> r = requests.get('https://api.twitch.tv/kraken/streams/test_channel',
headers={'Accept': 'application/vnd.twitchtv.v2+json'})
>>> print(r.status_code)
422 # <- on my machine, YMMV
>>> print(r.text)
{"status":422,"message":"Channel 'test_channel' is not available on Twitch",
"error":"Unprocessable Entity"}
I usually use urllib2 for my api requests in (blocking) python apps.
>>> import urllib2
>>> req = urllib2.Request('https://api.twitch.tv/kraken/streams/test_channel', None, {'Accept':'application/vnd.twitchtv.vs+json'})
>>> response = urllib2.urlopen(req)
You can then access the text returned with response.read(). From there you can parse the JSON with your preferred library, though I generally just use json.loads(response.read()).
I would keep in mind, though, that this is for 2.7, if you are using python 3 the libraries have been moved around and this can be found in urllib.request