0

Using requests

@client.command()
async def command(ctx, search=""):
    response = requests.get('URL'+search)    
    data = response.json()
    await ctx.send(data)

But whenever typing the command in discord I have to type %20 whenever there is a space. Is there any way to do that automatically in the code?

Kev
  • 9
  • 2
  • You asked the same question before and it got closed. Instead of making a new account and asking it again, please take a look at the referenced duplicate question (https://stackoverflow.com/questions/1695183/how-to-percent-encode-url-parameters-in-python), you pretty much got the same answer here. – Łukasz Kwieciński Jun 29 '21 at 15:14

2 Answers2

1

I would advice in using urllib.parse.quote instead of string.replace() To enable the use unicode

For example using this El Niño should result into /El%20Ni%C3%B1o/ which is near impossible to do using string.replace()

from urllib.parse import quote

@client.command()
async def command(ctx, search=""):
    response = requests.get('URL'+ quote(search, safe=''))    
    data = response.json()
    await ctx.send(data)
Abdulaziz
  • 3,363
  • 1
  • 7
  • 24
0

Yes there is. You could use the string.replace() method.

@client.command()
async def command(ctx, search=""):
    response = requests.get('URL'+search.replace(" ", "%20"))    
    data = response.json()
    await ctx.send(data)
itzFlubby
  • 2,269
  • 1
  • 9
  • 31