![](/img/trans.png)
[英]What is the standard for 'application/x-www-form-urlencoded' and an empty value?
[英]Tastypie with application/x-www-form-urlencoded
我在確定下一步應該是什么時遇到了一些困難。 我正在使用tastypie為我的Web應用程序創建API。
從另一個應用程序,特別是ifbyphone.com,我收到一個沒有標題的POST,看起來像這樣:
post data:http://myapp.com/api/
callerid=1&someid=2&number=3&result=Answered&phoneid=4
現在,我在我的服務器日志中看到這是在擊中我的服務器。但是tastypie抱怨POST的格式。
{“error_message”:“指示'application / x-www-form-urlencoded'的
formats
沒有可用的反序列化方法。請檢查您的Serializer上的formats
和content_types
。”,“traceback”:“Traceback(最近一次調用最后一次) :\\ n \\ n File \\“/ usr / local / lib / python2.7 / dist-packages / tastypie / resources.py \\”
當我嘗試使用curl發布原始數據時,我也收到相同的消息,我“相信”與ifbyphone的POST方法使用的基本過程相同:
curl -X POST --data 'callerid=1&someid=2&number=3&duration=4&phoneid=5' http://myapp.com/api/
因此,假設我的問題實際上是錯誤消息中指定的內容,並且沒有反序列化方法,我將如何編寫一個?
在這個提交的幫助下( https://github.com/toastdriven/django-tastypie/commit/7c5ea699ff6a5e8ba0788f23446fa3ac31f1b8bf )我一直在編寫自己的序列化程序,從文檔中復制基本框架( https:// django -tastypie.readthedocs.org/en/latest/serialization.html#implementing-your-own-serializer )
import urlparse
from tastypie.serializers import Serializer
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
當我編輯我的資源模型以實際使用我創建的序列化程序類時,這可以正常工作。 這在文檔中並不清楚。
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
我會在Brandon Bertelsen的帖子中添加對from_urlencode的修改,以便更好地使用國際字符:
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
我不確定這是不是因為我身邊的環境原因,但我發現當使用以下字符串“ÁáÄäÅåÉéÍíÑñÓóÖöÚúÜü”和原始功能時,我遇到了一些問題。
當該字符串被URL編碼時,它變成:“%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”
當它被URL解碼時,我們有: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”
這里的問題是這個字符串似乎是unicode,但它實際上不是,所以上面的字符串轉換為:“ÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÔ
我發現,如果我將URL解碼后的值解釋為latin-1,然后將其解碼為UTF-8,我就會得到正確的原始字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.