Tastypie with application/x-www-form-urlencoded

烂漫一生 提交于 2019-12-05 07:10:58

This worked as expected when I edited my resource model to actually use the serializer class I created. This was not clear in the documentation.

class urlencodeSerializer(Serializer):
    formats = ['json', 'jsonp', 'xml', 'yaml', 'html', 'plist', 'urlencode']
    content_types = {
        'json': 'application/json',
        'jsonp': 'text/javascript',
        'xml': 'application/xml',
        'yaml': 'text/yaml',
        'html': 'text/html',
        'plist': 'application/x-plist',
        'urlencode': 'application/x-www-form-urlencoded',
        }
    def from_urlencode(self, data,options=None):
        """ handles basic formencoded url posts """
        qs = dict((k, v if len(v)>1 else v[0] )
            for k, v in urlparse.parse_qs(data).iteritems())
        return qs

    def to_urlencode(self,content): 
        pass

MyModelResource(ModelResoucre):
    class Meta:
        ...
        serializer = urlencodeSerializer() # IMPORTANT

I would add a modification to the from_urlencode mentioned in Brandon Bertelsen's post to work better with international characters:

def from_urlencode(self, data, options=None):
    """ handles basic formencoded url posts """
    qs = {}
    for k, v in urlparse.parse_qs(data).iteritems():
        value = v if len(v)>1 else v[0]
        value = value.encode("latin-1").decode('utf-8')
        qs[k] = value
    return qs

I'm not sure if this is because of an environmental reason on my side, but I found that when using the following string "ÁáÄäÅåÉéÍíÑñÓóÖöÚúÜü" and the original function, I ran into some problems.

When this string gets URL encoded, it turns into: "%C3%81%C3%A1%C3%84%C3%A4%C3%85%C3%A5%C3%89%C3%A9%C3%8D%C3%AD%C3%91%C3%B1%C3%93%C3%B3%C3%96%C3%B6%C3%9A%C3%BA%C3%9C%C3%BC"

When this gets URL decoded, we have: u'\xc3\x81\xc3\xa1\xc3\x84\xc3\xa4\xc3\x85\xc3\xa5\xc3\x89\xc3\xa9\xc3\x8d\xc3\xad\xc3\x91\xc3\xb1\xc3\x93\xc3\xb3\xc3\x96\xc3\xb6\xc3\x9a\xc3\xba\xc3\x9c\xc3\xbc'

The problem here is that this string appears to be unicode, but it actually isn't, so the above string gets converted to: "ÃáÃäÃÃ¥ÃéÃíÃñÃóÃÃ"

I found that if I interpreted the URL decoded value as latin-1, and then decoded it for UTF-8, I got the correct original string.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!