0
votes

I'm having the following problem: when I use SQSConnection.send_message method with a fixed string as a parameter (with no accented characters), it works as expected. But when I get the body of a message (using get_messages) and try to send it again to the same queue, I get this error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xea' in position 38: ordinal not in range(128)

The messages were written directly from Amazon Web Console and have a few ";" characters and some accented such as "õ" and "ã". What should I do? I'm already using set_message_class(RawMessage) as suggested here

Using python BOTO with AWS SQS, getting back nonsense characters

but it only worked for receiving the messages. I'm using Ubuntu 12.04, with python-boto installed from repositories (I think it's version 2.22, but don't know how to check).

Thanks!!

1

1 Answers

2
votes

send_message can only handle byte strings (str class). What you are receiving from SQS is a Unicode string (unicode class). You need to convert your Unicode string to a byte string by calling encode('utf-8') on it.

If you have a mix of string types coming in you may need to conditionally encode the Unicode string into a byte string.

    if type(message_body) is unicode:
        message_content = message_body.encode('utf-8')
    else:
        message_content = message_body