# String Encoding / Importing Feeds

**URL:** <https://www.ruby-forum.com/t/string-encoding-importing-feeds/105244>\
**Category:** Rails\
**Created:** [May 27, 2007, 10:56pm UTC](https://www.ruby-forum.com/t/string-encoding-importing-feeds/105244 "2007-05-27T22:56:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hunter\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@hunter\_h](https://www.ruby-forum.com/u/hunter_h)\
**Post date:** [May 27, 2007, 10:56pm UTC](https://www.ruby-forum.com/t/string-encoding-importing-feeds/105244/1 "2007-05-27T22:56:58Z")

</div>

Howdy,

I have a Rails app that grabs from RSS feed info and then tries to  
insert it into a database.

The problem I’m having is that some feeds appear to use funky  
characters and my INSERTs are failing. The actual error is below.

Any idea how I can make this work reliably, not really knowing if a  
feed will have these or not?

I’m using PostgreSQL and the DB was initialized as UTF-8.

Any help is very much appreciated.

feeds#update\_feeds (ActiveRecord::StatementInvalid) “PGError: ERROR:  
invalid byte sequence for encoding "UTF8": 0xb9\n: INSERT INTO  
feed\_items ("item\_id", "updated\_at", "title", "item\_updated",  
"description", "feed\_id", "item\_link", "created\_at") VALUES  
(NULL, ‘2007-05-27 20:40:32.433422’, ‘Quote du jour’, ‘2007-05-25  
15:57:55.000000’, ‘

\<a href="[http://www.blah.lhah.com](http://www.blah.lhah.com)"\>My Name\</  
a\>: _There\271s really only one rule for community as far as I\271m  
concerned, and it\271s this - in order to call some gathering of  
people a "community", it is a requirement that if you\271re a  
member of the community, and one day you stop showing up, people will  
come looking for you to see where you went._

’, 6, ‘feed\_url’,  
‘2007-05-27 20:40:32.433422’)”

---

<div class="post-metadata">

**Author:** ![hunter\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@hunter\_h](https://www.ruby-forum.com/u/hunter_h)\
**Post date:** [May 27, 2007, 11:27pm UTC](https://www.ruby-forum.com/t/string-encoding-importing-feeds/105244/2 "2007-05-27T23:27:00Z")

</div>

You need to encode your string rather than using it raw. Then reverse  
the process on read.

Probably the easiest and safest is to use base64 encoding, or to place  
the string in a blob rather than a string field.

Michael

---

<div class="post-metadata">

**Author:** ![hunter\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@hunter\_h](https://www.ruby-forum.com/u/hunter_h)\
**Post date:** [June 1, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/string-encoding-importing-feeds/105244/3 "2008-06-01T20:28:06Z")

</div>

Hunter,  
Did you ever find a solution to this problem?  
I am having very similar issues:

RAW RESPONSE TEXT:  
[Salut Alex, Ã©coute c’est Alex je teste un peu et puis bonjour franÃ§ais  
franÃ§aises. ]  
UTF-8 Response text:  
[Salut Alex, Ã©coute c’est Alex je teste un peu et puis bonjour franÃ§ais  
franÃ§aises. ]  
Unique Id: 1212337033-59  
SQL (0.000081) BEGIN  
GlobalInbox Update (0.000000) PGError: ERROR: invalid byte sequence  
for encoding “UTF8”: 0xe9636f  
HINT: This error can also happen if the byte sequence does not match the  
encoding expected by the server, which is controlled by  
“client\_encoding”.  
: UPDATE global\_inboxes SET “created\_at” = ‘2008-06-01 12:17:13.416834’,  
“voicemail\_status\_id” = 4, “deleted\_at” = NULL, “voicemail\_folder\_id” =  
1, “deleted” = ‘f’, “sender\_cid” = ‘953794484’, “conversion\_to\_text” =  
'Salut Alex, Ã©coute c’est Alex je teste un peu et puis bonjour franÃ§ais  
franÃ§aises. ', “notes” = NULL, “voicemail\_id” = 491, “updated\_at” =  
‘2008-06-01 12:18:09.957716’, “user\_id” = 28 WHERE “id” = 491

The output above is from my production.log file.  
The RAW response is the text that is output to the console by simply  
printing the string with the data.  
the UTF-8 is the .chars method called on the string type.

I am unable to insert the data into the DB using a string type or .chars  
method.

Did you find a solution?

Hunter H. wrote:

> Howdy,
> 
> I have a Rails app that grabs from RSS feed info and then tries to  
> insert it into a database.
> 
> The problem I’m having is that some feeds appear to use funky  
> characters and my INSERTs are failing. The actual error is below.
> 
> Any idea how I can make this work reliably, not really knowing if a  
> feed will have these or not?
> 
> I’m using PostgreSQL and the DB was initialized as UTF-8.
> 
> Any help is very much appreciated.
> 
> feeds#update\_feeds (ActiveRecord::StatementInvalid) “PGError: ERROR:  
> invalid byte sequence for encoding "UTF8": 0xb9\n: INSERT INTO  
> feed\_items ("item\_id", "updated\_at", "title", "item\_updated",  
> "description", "feed\_id", "item\_link", "created\_at") VALUES  
> (NULL, ‘2007-05-27 20:40:32.433422’, ‘Quote du jour’, ‘2007-05-25  
> 15:57:55.000000’, ‘
> 
> \<a href="[http://www.blah.lhah.com](http://www.blah.lhah.com)"\>My Name\</  
> a\>: _There\271s really only one rule for community as far as I\271m  
> concerned, and it\271s this - in order to call some gathering of  
> people a "community", it is a requirement that if you\271re a  
> member of the community, and one day you stop showing up, people will  
> come looking for you to see where you went._
> 
> ’, 6, ‘feed\_url’,  
> ‘2007-05-27 20:40:32.433422’)”
