Typeerror: POST data should be bytes, an iterable of bytes, or a file Object. It cannot be of type str

Asked

Viewed 1,118 times

1

I’m trying to translate a code from Python 2.7 to 3.6

Code 2.7 works perfectly:

from urllib2 import Request, urlopen

values = """
   {
    "exchange_code": "PLNX",
    "exchange_market": "BTC/USDT"
   }
"""

headers = {
'Content-Type': 'application/json',
'X-API-KEY': 'xxxxxx',
'X-API-SECRET': 'yyyyyy'
}

request = Request('https://api.coinigy.com/api/v1/ticker', data=values, headers=headers)
response_body = urlopen(request).read()
print response_body

The code to 3.6 :

from urllib.request import Request, urlopen

values = """
   {
    "exchange_code": "PLNX",
    "exchange_market": "BTC/USDT"
   }
"""

headers = {
'Content-Type': 'application/json',
'X-API-KEY': 'xxxxxx',
'X-API-SECRET': 'yyyyyy'
}

request = Request('https://api.coinigy.com/api/v1/ticker', data=values, headers=headers)
response_body = urlopen(request).read()
print(response_body)

Returns the following error :


Typeerror Traceback (Most recent call last) in () 1 request = Request('https://api.coinigy.com/api/v1/markets', values, headers=headers) -----> 2 response_body = urlopen(request) 3 print(response_body)

C: Users Marcelo Anaconda3 lib urllib request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context) 221 And: 222 opener = _opener --> 223 Return opener.open(url, data, timeout) 224 225 def install_opener(opener):

C: Users Marcelo Anaconda3 lib urllib request.py in open(self, fullurl, data, timeout) 522 for Processor in self.process_request.get(Protocol, []): 523 meth = getattr(Processor, meth_name) --> 524 req = meth(req) 525 526 Sponse = self. _open(req, date)

C: Users Marcelo Anaconda3 lib urllib request.py in do_request_(self, request) 1246 msg = "POST data should be bytes, an iterable of bytes, " 1247 "or a file Object. It cannot be of type str." -> 1248 raise Typeerror(msg) 1249 if not request.has_header('Content-type'): 1250 request.add_unredirected_header(

Typeerror: POST data should be bytes, an iterable of bytes, or a file Object. It cannot be of type str.

From what I understand I have to pass the parametors to urllib.request in bytes format and not in str format, but I do not know which is the correct way to convert the values.

Thanks for the help.

1 answer

2


Try this code:

from urllib.request import Request, urlopen

values = """
   {
    "exchange_code": "PLNX",
    "exchange_market": "BTC/USDT"
   }
"""

headers = {
    'Content-Type': 'application/json',
    'X-API-KEY': 'xxxxx',
    'X-API-SECRET': 'yyyyyy'
}

request = Request('https://api.coinigy.com/api/v1/ticker', data=values.encode('utf-8'), headers=headers)
response_body = urlopen(request).read()
print(response_body)

The problem apparently was in the way you were dealing with the dictionary. It was necessary to encode the values before.

Of the documentation (English):

For an HTTP POST request method, data should be a buffer in the standard application/x-www-form-urlencoded format. The urllib.parse.urlencode() Function takes a Mapping or Sequence of 2-tuples and Returns an ASCII string in this format. It should be encoded to bytes before being used as the data Parameter.

Translation of the highlighted excerpt:

The parameter must be converted to bytes before being used as parameter of the date variable.

  • could translate this highlighted passage into English?

  • @diegofm ready. Best?

  • The error msg no longer appears however the answer of the call is : b'{"err_num":"1057-32-01","err_msg":"Missing or Empty Parameters:"}'

  • @Risktech I just tested in Python 3.6.0 and got this answer: b'{"data":[{"exchange":"PLNX","market":"USDT /BTC","last_trade":"1115.0200000000","high_trade":"1123.4000000000","low_trade":"1026.1450002000","current_volume":"8157.2646000000","timestamp":"201 7-03-21 20:23:53","Ask":"1115.9999999900","Bid":"1115.0100000000"}],"Notifications":[]}' Can test again?

  • It really worked, it may be that the feed was down when I tested it. Thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.