Skip to content

Fix UnicodeEncodeError in urllib if non-ascii characters are present in Request parameters#91

Closed
romanbarczynski wants to merge 1 commit into
joestump:masterfrom
romanbarczynski:master
Closed

Fix UnicodeEncodeError in urllib if non-ascii characters are present in Request parameters#91
romanbarczynski wants to merge 1 commit into
joestump:masterfrom
romanbarczynski:master

Conversation

@romanbarczynski

Copy link
Copy Markdown

Request.to_postdata was fixed some time ago, but Request.to_url was still unable to create url when unicode characters were present in any of Request parameters (because of UnicodeEncodeError)

See included test.

@8maki

8maki commented Oct 2, 2011

Copy link
Copy Markdown

I have the same problem now.
Please pull this!

@atomatt

atomatt commented Oct 14, 2011

Copy link
Copy Markdown

Just hit this problem too, and can confirm that romke's commit fixes things.

@atomatt

atomatt commented Oct 14, 2011

Copy link
Copy Markdown

Also, that method seems awfully complicated and can probably be replaced with the following if you prefer:

    def to_url(self):
        """Serialize as a URL for a GET request."""
        scheme, netloc, path, query, fragment = urlparse.urlsplit(self.url.encode('utf-8')) 
        query = parse_qs(query)                                                             
        for k, v in self.iteritems():                                                       
            query.setdefault(k.encode('utf-8'), []).append(to_utf8_optional_iterator(v))    
        query = urllib.urlencode(query, True)                                               
        return urlparse.urlunsplit((scheme, netloc, path, query, fragment))                 

@brunsgaard

Copy link
Copy Markdown

Thank you romke for the patch, works like charm..

Someone should pull this into master, the patch has been available for over a year now..

@marians

marians commented Jul 10, 2013

Copy link
Copy Markdown

Is there any update on this? I seem to run into this problem.

@bxm156

bxm156 commented Oct 27, 2013

Copy link
Copy Markdown

Thank you!

@yjlou

yjlou commented Jul 13, 2015

Copy link
Copy Markdown

Thanks emgee. Your fix saves my code.

@joestump joestump closed this in 7d0b8d7 Jul 29, 2015
@joestump

Copy link
Copy Markdown
Owner

Your patch was half-fixed in a different PR. I modified your PR to use to_utf8() instead, but kept your test as-is, which passes. See 7d0b8d7 for the details.

@joestump joestump added this to the 2.0 milestone Jul 29, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants