Skip to content
Advertisement

how to get a apis output in python without json

here is api i am trying https://api.apithis.net/host2ip.php?hostname=google.com as you can see unlike json apis it dose not have “” that you can copy as response.json

@client.command()
async def host(ctx, host):
    url = ('https://api.apithis.net/host2ip.php?hostname=' + host)

    response = requests.get(url)
    ipaddress = response.json()

    embed = discord.Embed(title="IP for website" + host, color=0x00ffff)
    embed.add_field(name="IP:", value=f'{ipaddress}', inline=True)                                                            
    
    await ctx.send(embed=embed)

here is my current code if anyone can fix would help alot as im not sure how to use apis without json output thanks

Advertisement

Answer

Firstly the response you are getting is not JSON. If you see the response header then you can see it is content-type: text/html; charset=UTF-8.

Now that it is not JSON you have to treat it as text.

r = requests.get(url)
ipaddress = r.text if r.status_code == 200 else ''
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement