{"id":4135,"date":"2016-07-14T03:20:45","date_gmt":"2016-07-14T07:20:45","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=4135"},"modified":"2018-04-03T10:37:13","modified_gmt":"2018-04-03T14:37:13","slug":"python-mechanize-replace","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/python-mechanize-replace\/","title":{"rendered":"Urllib&#8217;s urlencode: The weird case of %22 and %27"},"content":{"rendered":"<p>When I was doing some API testing using Python Mechanize, I struck an internal server error. I tried to find the root cause of the error and I realized that Python&#8217;s urllib is doing something weird when encoding strings. It is replacing double quotes with single quotes. I spent more than hour to debug this issue before I realized what was wrong. So I am writing this post to help testers to find the solution for this HTTP error and not spend much time on this.<\/p>\n<h3>The problem<\/h3>\n<p>Let us see an example of the problem:<\/p>\n<pre lang=\"python\">\r\n Import urllib\r\n a = {\"name\":\"tester\",\"company\":{\"id\":\"1\",\"work\":\"QA\"}}\r\n data = urllib.urlencode(a)\r\n print data\r\n<\/pre>\n<p style=\"text-align:left\">\n<b>Expected output:<\/b>company=%7B%22work%22%3A+%22QA%22%2C+%22id%22%3A+%221%22%7D&amp;name=tester<br \/>\n<b>Actual output:  <\/b>company=%7B%27work%27%3A+%27QA%27%2C+%27id%27%3A+%271%27%7D&amp;name=tester\n<\/p>\n<p>Here you can see that , %7B%27 etc are urlencoded parameters. When I  check in encoding reference, encoded value of single quote is %27 and double quote is %22.So urllib seems to have replaced the %22 with %27.<\/p>\n<p>Due to this, the server can\u2019t recognize what I am sending in request and it throws an internal server error. In the interest of time, I simply replaced %27 with %22 in the encoded string.<\/p>\n<pre lang=\"python\">\r\nImport urllib\r\na = {\"name\":\"tester\",\"company\":{\"id\":\"1\",\"work\":\"QA\"}}\r\ndata = urllib.urlencode(a)\r\nd1 = data.replace(\u2018%27\u2019,\u2019%22\u2019)\r\nprint d1\r\n<\/pre>\n<p><b>Output:<\/b>company=%7B%22work%22%3A+%22QA%22%2C+%22id%22%3A+%221%22%7D&amp;name=tester<\/p>\n<p>Now you can see that %27 is replaced by %22.<\/p>\n<p>This is the solution I used to get over the internal server error. But it seems like an ugly hack. If any one of you find a better solution for this problem please let me know.<\/p>\n<p><strong>If you liked this article, learn more <a href=\"https:\/\/qxf2.com\/blog\/about-qxf2\/\">about Qxf2&#8217;s<\/a> testing services for startups.<\/strong><\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>When I was doing some API testing using Python Mechanize, I struck an internal server error. I tried to find the root cause of the error and I realized that Python&#8217;s urllib is doing something weird when encoding strings. It is replacing double quotes with single quotes. I spent more than hour to debug this issue before I realized what [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,18],"tags":[],"class_list":["post-4135","post","type-post","status-publish","format-standard","hentry","category-api-testing","category-python"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/4135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=4135"}],"version-history":[{"count":14,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/4135\/revisions"}],"predecessor-version":[{"id":4501,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/4135\/revisions\/4501"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=4135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=4135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=4135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}