Pass variable to params unirest

781 views Asked by At

I have a function which returns a list of text. I am using an API from Mashape (Maui-Pro) which generates keywords from a given piece of text. Mashape requires that you use unirest for HTTP requests. I want to pass the list of text to the API and generate keywords for each piece of text but can't work out how to loop through the each piece of text in the list using unirest. Here's the code:

import unirest
from pprint import pprint
from get_articles import extract_text

def get_keywords():

    text_list = extract_text()

    response = unirest.post("https://maui-v1.p.mashape.com/api/keywords",
        headers={"X-Mashape-Key": "UbvKOrgO0amshGoyNHDgGtxaO72vp1ck58Gjsn5BzPZADqHBtb", "Content-Type": "application/json", "Accept": "application/json"},
        params=("{\"return_translations\":false," "\"return_uris\":false," "\"content\":\"A piece of text from which to return keywords.\"," "\"thesaurus\":\"English Wikipedia\"}"))

    print(response.__dict__)

    get_keywords()

I'd like to substitute the content text in params with each piece of text in text_list and return keywords for each. Any help much appreciated. Thanks!

1

There are 1 answers

0
sammy88888888 On BEST ANSWER

Not so much an answer, but my suggestion if anyone else has this problem is to use the python Requests module. Unirest is just messy! The API provider answered a similar query from me suggesting the following:

Opening and reading a file in python is independent of unirest or this API. For example:

content = ""
with open('Path/to/file', 'r') as content_file:
   content = content_file.read()

will read the content in a way that can then be used with this or any other API However, I was unable to get around. Perhaps you succeeded. Can answer with an example of the code?