通过查询字符串发送字符串数组/列表的问题

时间:2014-10-22 05:25:53

标签: python python-requests

我正在尝试将字符串数组发送到bing translate api,我已经收到并编码为UTF-8。 但是,当我使用变量进行调用时,服务会发出错误:

“反序列化System.String []类型的对象时出错。遇到意外字符'ï'。”

如果我手动将字符串数组添加为不在变量中的字符串,则可以正常工作。我正在使用Requests模块,所以我认为它正在编码网址...我不知道。

class getLanguages(apiFunctions):
    def GET(self):
        #get the access token
        data = self.accessToken()
        token = data['access_token']

        codes = self.getCodes(token)
        languageNames = self.getLanguageNames(token,codes)


        codesList = ast.literal_eval(codes)

        return languageNames + codes

    def getCodes(self,token):
        url = 'http://api.microsofttranslator.com/V2/Ajax.svc/GetLanguagesForTranslate'
        auth_header = {'Authorization': 'Bearer ' + token}
        r = requests.get(url, headers=auth_header)
        return r.text.encode('utf-8')

    def getLanguageNames(self,token,codes):
        url = 'http://api.microsofttranslator.com/V2/Ajax.svc/GetLanguageNames'

        #this is where I add the language codes string to the query

        payload = {'locale': 'en', 'languageCodes': codes}
        auth_header = {'Authorization': 'Bearer ' + token}
        r = requests.post(url, params=payload, headers=auth_header)

        return r.text.encode('utf-8')

这是字符串:

["ar","bg","ca","zh-CHS","zh-CHT","cs","da","nl","en","et","fi","fr","de","el","ht","he","hi","mww","hu","id","it","ja","tlh","tlh-Qaak","ko","lv","lt","ms","mt","no","fa","pl","pt","ro","ru","sk","sl","es","sv","th","tr","uk","ur","vi","cy"]

0 个答案:

没有答案