-3

Using python 3.7

I'm trying to build a small script that will post data using an api however I'm not able to get the response.content properly because there's some strange characters coming out.

import requests
files = {
    'content': (None, 'text'),
}
response = requests.post('http://localhost/api/v2', files=files)
print (response.content)

Outputs: b'http://localhost/1NSZAOWE\n'

I tried erasing characters doing

txt = (response.content)
print (txt[1:])

Outputs: b'ttp://localhost/2LSALWE\n'

Can someone help me finding out a way to avoid getting those odd characters in the url that comes out as response.content?

IceeFrog
  • 327
  • 3
  • 16

1 Answers1

1

response.content is returning byte string so you have to convert it to string(utf-8) and use strip() to remove tailing newline

response.content.decode('utf-8').strip()

check Convert bytes to a string

Vikas Mulaje
  • 727
  • 6
  • 11
  • Found out with @Marcin comment, thanks for the answer, really new to python syntax but I get the references – IceeFrog Apr 24 '20 at 00:46