0

Can someone please let me know how to make requests to Wit.ai message api. I am struggling with my code.

import requests
import json
import sys
from wit import Wit


# Wit speech API endpoint
API_ENDPOINT = 'https://api.wit.ai/message'

q='who are you'

# Wit.ai api access token
wit_access_token = 'B3GHXHLTXIASO7S4KY7UC65LMSTCDEHK'

    # defining headers for HTTP request
headers = {'authorization': 'Bearer ' + wit_access_token}

    # making an HTTP post request
resp = requests.post(API_ENDPOINT, headers = headers,data = {'q':'who are you'})

    # converting response content to JSON format
data = json.loads(resp.content)

print(data)

I am getting back this:

{u'code': u'json-parse', u'error': u'Invalid JSON'}
user3289968
  • 97
  • 2
  • 12
  • Try writing Authorization with capital A. Also send it as a .get. According to their docs you don't need a .post. So with that done call it like this: resp = requests.get(API_ENDPOINT, headers=headers, params={'q': 'who are you'}). – jlaur Jul 16 '17 at 22:39

1 Answers1

3

The /message endpoint of Wit API accepts only GET method and expects the query parameters in URL (not data in request body). The requests library will correct the case on your lowercase Authorization header field, but it's a good practice to write it according to the standard. Also, JSON response can be fetched decoded with json() method on Response.

With all of this:

import requests

API_ENDPOINT = 'https://api.wit.ai/message'
WIT_ACCESS_TOKEN = 'B3GHXHLTXIASO7S4KY7UC65LMSTCDEHK'

headers = {'Authorization': 'Bearer {}'.format(WIT_ACCESS_TOKEN)}
query = {'q': 'who are you'}

resp = requests.get(API_ENDPOINT, headers=headers, params=query)
data = resp.json()

print(data)

Note however, Wit has a Python library which abstracts all of these low-level details, and makes your code much simpler and easier to read. Use it.

It would look like this (example from the docs):

from wit import Wit
client = Wit(access_token=WIT_ACCESS_TOKEN)
resp = client.message('what is the weather in London?')
print('Yay, got Wit.ai response: ' + str(resp))
Community
  • 1
  • 1
randomir
  • 17,989
  • 1
  • 40
  • 55
  • Tried using the below code but still getting error from wit import Wit WIT_ACCESS_TOKEN = 'B3GHXHLTXIASO7S4KY7UC65LMSTCDEHK' client = Wit(WIT_ACCESS_TOKEN) resp = client.message('who are you?') print('Yay, got Wit.ai response: ' + str(resp)) Got: Traceback (most recent call last): File "C:/Python27/wit_try.py", line 4, in resp = client.message('who are you?') AttributeError: 'Wit' object has no attribute 'message' – user3289968 Jul 17 '17 at 15:48
  • I tested the first code sample above (with requests lib), that works for sure. The second sample I (mostly) copied from their documentation example, and haven't tested it. You can try the full example they provide in the docs, please see the link from my answer. – randomir Jul 17 '17 at 21:38
  • The first code sample worked well. Thanks for that!!! I also first tried using the Wit message API. But the result was same as posted above. I have another question running regarding it but nobody answered yet. Can you please test it? https://stackoverflow.com/questions/45121612/wit-object-has-no-attribute-message – user3289968 Jul 18 '17 at 16:08
  • Glad it worked. Please also consider upvoting it and closing the question by accepting the answer. – randomir Jul 18 '17 at 16:34
  • Yes definitely, can you check why the example from the docs not working, I would really like to you the example code – user3289968 Jul 19 '17 at 18:38