HttpResponse Object-json object must is str, not ' bytes '

Source: Internet
Author: User

Http://stackoverflow.com/questions/24069197/httpresponse-object-json-object-must-be-str-not-bytes

HttpResponse Object-json object must is str, not ' bytes '
Up vote17down votefavorite7

I ' ve been trying to update a small Python library called Libpynexmo to work with Python 3.

I ' ve been stuck on the This function:

DefSend_request_json(Self,Request):Url=Request Req=Urllib.Request.Request(Url=Url)Reqadd_header ( ' Accept ' ,  ' Application/json ' )  try : return Json.load (urllib. Request. Urlopen (req except< Span class= "pln" > valueerror: return false            

When the it gets to this, the JSON responds With:

TypeError: the JSON object must be str, not ‘bytes‘

I read in a few places so for you json.load should pass objects (in this case an HTTPResponse Object) with a .read() attached, but it Doesn ' t work on HTTPResponse Objects.

I ' m at a loss as to where to go with this next, but being that my entire the line script was freshly converted to Python 3 , I don ' t feel like going back to 2.7.

JSON python-3.x python-3.4 Nexmo
ShareImprove this question edited  7 ' in 4:01 emrakul3,7315 2659 Asked June 5 ' at 19:59Chevron129
5
See here for a Solution:stackoverflow.com/questions/6862770/...–dano June 5 ' in 20:03
Did you try passing it through 2to3? –zmo June 5 ' + at 20:03
@zmo-did it manually so I could learn More. –chevron June 5 ' + at 20:24
@dano-found that link earlier, but is unable to make his workaround apply to my Situation. I am unable to use. ReadAll () on my HttpResponse object. –chevron June 5 ' + at 20:26
2
@Chevron, If you ' re trying to convert the JSON request object and then use This:json.loads (request.b? ody.decode (' utf-8 ')) – Anshuman Biswas ' at 0:37
Show1More Comment
4 answersactiveoldestvotes
Up vote13down voteaccepted

I recently wrote a small function to send Nexmo Messages. Unless need the full functionality of the Libpynexmo code, this should does the job for YOU. And if you want to continue overhauling libpynexmo, just copy this Code. The key is UTF8 encoding.

If you want to send any other fields with your message, the full documentation for what can include with a Nexmo OUTBO Und message is Here

Python 3.4 tested Nexmo Outbound (JSON):

DefNexmo_sendsms(Api_key,Api_secret,Sender,Receiver,Body): "" "sends a message using NEXMO.:P Aram Api_key:nexmo provided API Key:p Aram Api_secret:nexmo provided secrety key:p ar AM sender:the number used to send the Message:p Aram receiver:the number The message was addressed to:p Aram Body:the me Ssage Body:return:Returns the MsgId received back from NEXMO after message has been Sent. """Msg= { ' Api_key ':Api_key, ' Api_secret ':Api_secret, ' From ':Sender, ' To ':Receiver, ' Text ':Body}Nexmo_url= ' Https://rest.nexmo.com/sms/json 'Data=Urllib.Parse.UrlEncode(Msg)Binary_data=Data.Encode(' UTF8 ')Req=Urllib.Request.Request(Nexmo_url,Binary_data) response = Urllib.. Urlopen (req)  result =< Span class= "pln" > Json. (response. Readall ().  Decode ( ' utf-8 '   Result[ ' messages ' ][ 0][ ' Message-id '       
ShareImprove this answer Edited June "at 20:51aereaux65016 answered June 6 ' at 1:01MiR3934
Add a Comment
Up vote30down vote

Facing the same problem I solve it using decode ()

   ... rawreply = Connection. ().  Read () reply = Json.< Span class= "pln" >loads (rawreply. Decode ())             
shareimprove This answer answered Jul 1 ' + at 18:41 costas309 12 3
13
Can You explain why this is necessary? –skaz June ' at 8:45
Add a Comment
Up vote5down vote

I met the problem as well and now it pass

ImportJsonImportUrllib.RequestAs Urimport Urllib.as parhtml = Ur.< Span class= "pln" >urlopen (url () print (type< Span class= "pun" > (htmldata =  Json. (html. Decode ( ' utf-8 '       

HttpResponse Object-json object must is str, not ' bytes '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.