Python Forum
Unable to convert request response.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to convert request response.
#1
Hi,

I'm trying to convert a response which I think is in Unicode? I am using the requests library to get data from an API. I am running the following:

import requests

url = "https://api-football-v1.p.rapidapi.com/v3/standings"

querystring = {"season":"2020","league":"187"}

headers = {
    'x-rapidapi-key': "asdfasdfasdfasdfasdf",
    'x-rapidapi-host': "api-football-v1.p.rapidapi.com"
    }

response = requests.request("GET", url, headers=headers, params=querystring)
strResponse = response.text
utf8string = strResponse.encode("utf-8")
print(utf8string)
It doesn't work though. In that response, I have the likes of:

{"id":10735,"name":"CR B\\u00e9ni Thour"}
But that should come back as:
{"id":10735,"name":"CR Béni Thour"}

Not sure what I'm doing wrong?

Many thanks!
J
Reply
#2
Usually so dos API give back json,so should not use text.
response = requests.request("GET", url, headers=headers, params=querystring)
json_data = response.json()
print(json_data)
So what you should get back here is Python dictionary,as Requests has build in encoding and decoding for json.
johnboy1974 likes this post
Reply
#3
(Apr-04-2021, 08:17 PM)johnboy1974 Wrote: Hi,

I'm trying to convert a response which I think is in Unicode? I am using the requests library to get data from an API. I am running the following:

import requests

url = "https://api-football-v1.p.rapidapi.com/v3/standings"

querystring = {"season":"2020","league":"187"}

headers = {
    'x-rapidapi-key': "asdfasdfasdfasdfasdf",
    'x-rapidapi-host': "api-football-v1.p.rapidapi.com"
    }

response = requests.request("GET", url, headers=headers, params=querystring)
strResponse = response.text
utf8string = strResponse.encode("utf-8")
print(utf8string)
It doesn't work though. In that response, I have the likes of:

{"id":10735,"name":"CR B\\u00e9ni Thour"}
But that should come back as:
{"id":10735,"name":"CR Béni Thour"}

Not sure what I'm doing wrong?

Many thanks!
J

It is not stored as you want it; it is stored in a version of UTF-8 encoding that uses \u to specify the character. It is up to you to provide a way to translate the \u codes to the actual Unicode characters. To do this, you have to make sure that Unicode is supported in the internal character set of strings. The external rendering requires that the encoding be supported, or that you convert it yourself. So you have to replace the characters \u00e9 with the character whose code is Unicode 00e9. Now, you can make a special case, because \u00XX is equivalent to XX, so even if your strings are only 8-bit strings, you could replace the sequece \u00XX with the character whose code is XX, but that only works for the special case of \u00, and is not a general scheme.
Reply
#4
(Apr-04-2021, 08:58 PM)snippsat Wrote: Usually so dos API give back json,so should not use text.
response = requests.request("GET", url, headers=headers, params=querystring)
json_data = response.json()
print(json_data)
So what you should get back here is Python dictionary,as Requests has build in encoding and decoding for json.

That worked a treat, mate!! Thanks so much. I'm clearly very bad at this :)

Cheers,
JOhn
Reply
#5
(Apr-05-2021, 05:58 AM)supuflounder Wrote: It is not stored as you want it; it is stored in a version of UTF-8 encoding that uses \u to specify the character. It is up to you to provide a way to translate the \u codes to the actual Unicode characters. To do this, you have to make sure that Unicode is supported in the internal character set of strings. The external rendering requires that the encoding be supported, or that you convert it yourself. So you have to replace the characters \u00e9 with the character whose code is Unicode 00e9. Now, you can make a special case, because \u00XX is equivalent to XX, so even if your strings are only 8-bit strings, you could replace the sequece \u00XX with the character whose code is XX, but that only works for the special case of \u00, and is not a general scheme.
The problem here is that did not use response.json().
response.text is the wrong way here and with that can get all kind of Unicode encoding problems Doh
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to request image from FORM Data usman 0 1,018 Aug-18-2022, 06:23 PM
Last Post: usman
  request.get to read large response pythonlearner1 7 3,299 Apr-05-2022, 08:21 PM
Last Post: pythonlearner1
  how can I correct the Bad Request error on my curl request tomtom 8 5,133 Oct-03-2021, 06:32 AM
Last Post: tomtom
  Empty response to request causing .json() to error t4keheart 1 10,157 Jun-26-2020, 08:35 PM
Last Post: bowlofred
  ImportError: cannot import name 'Request' from 'request' abhishek81py 1 3,972 Jun-18-2020, 08:07 AM
Last Post: buran
  Unable post request to AWS elasticsearch service Rupini 0 1,908 May-18-2020, 08:27 AM
Last Post: Rupini
  Issues parsing the response from a request garnold 3 2,597 May-14-2019, 12:39 PM
Last Post: snippsat
  Unable to print the exact Float values when I convert LIST to Sequence of Tuples? preethamalluri 1 2,478 Jul-12-2018, 09:03 AM
Last Post: buran
  unable to convert text file in dictionary purnima1 6 4,359 Apr-02-2018, 07:44 AM
Last Post: purnima1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020