django-pistonを使用しているPython辞書オブジェクトをJSON経由で呼び出し元に返す別のプログラマーが作成したコードをデバッグします。私はピストンhereの文書に従っています。応答形式を変更するとDjango-Pistonがtext/plainとしてJSON/Dictionaryを返す
私は手動でresponse = rc.CREATED
とresponse.content = my_dict
を設定した場合、レスポンスのコンテンツタイプが、私はresponse['Content-Type'] = 'application/json; charset=utf-8'
でそれを上書きしようとした場合でも、常にtext/plainであるいくつかの奇妙な行動に気づきました。
ここにいくつかのサンプルコードがあります。
class RequestHandler(BaseHandler):
'''API handler for translation request queries.'''
# we don't allow updating translation requests
allowed_methods = ('GET', 'POST', 'DELETE')
@throttle(MAX_REQUESTS_PER_MINUTE, 60)
def read(self, request, shortname = None, results = False):
'''Handles a GET request asking about translation requests.'''
not_deleted = TranslationRequest.objects.exclude(deleted = True)
not_deleted = not_deleted.filter(owner = request.user)
if shortname is None:
objects = not_deleted.all()
else:
try:
_request_uuid = uuid.UUID(shortname)
objects = [get_object_or_404(not_deleted, request_id=shortname)]
except ValueError:
objects = [get_object_or_404(not_deleted, shortname=shortname)]
objects = [ RequestHandler.request_to_dict(o, results)
for o in objects ]
if len(objects) == 1:
objects = objects[0]
return objects
@throttle(MAX_REQUESTS_PER_MINUTE, 60)
def create(self, request, shortname = None, results = False):
'''Handles a POST request to create a new translation request.'''
if shortname is not None or results:
return rc.BAD_REQUEST
print 'CREATE content-type', request.content_type # DEBUG
# get the data from the POST request
postdata = self.flatten_dict(request.POST)
# ensure that the worker field is present
postdata['worker'] = postdata.get('worker','')
# validate POST data using our Django form
form = TranslationRequestForm(request.user, postdata, request.FILES)
try:
if not form.is_valid():
return rc.BAD_REQUEST
except KeyError:
return rc.BAD_REQUEST
# create a new request object
new = TranslationRequest()
new.shortname = form.cleaned_data['shortname']
new.owner = request.user
new.worker = form.cleaned_data['worker']
# create a new worker message
message = TranslationRequestMessage()
message.request_id = new.request_id
message.source_language = form.cleaned_data['source_language']
message.target_language = form.cleaned_data['target_language']
message.source_text = u''
for chunk in request.FILES['source_text'].chunks():
message.source_text += unicode(chunk, 'utf-8')
handle = open('{0}/{1}.message'.format(TRANSLATION_MESSAGE_PATH,
new.request_id), 'w+b')
handle.write(message.SerializeToString())
handle.close()
new.save()
new.start_translation()
messages.add_message(request, messages.SUCCESS, 'Successfully ' \
'started translation request "{0}".'.format(
new.shortname))
# return 201 CREATED
response = rc.CREATED
# put the URI of the newly created object into the HTTP header
# Location field (see RFC 2616)
response['Content-Type'] = 'application/json; charset=utf-8'
response['Location'] = reverse('requests', args=[new.request_id + '/'])
# echo the created object inside the HTTP response
# NOTE: this overwrites the "Location" header field set above.
# See piston.resource.__call__()
response.content = RequestHandler.request_to_dict(new, include_results=False)
return response
@staticmethod
def request_to_dict (request, include_results = False):
'''Transforms a TranslationRequest object to a Python
dictionary.'''
retval = {}
retval['owner'] = request.owner.username
retval['shortname'] = request.shortname
retval['worker'] = request.worker.shortname
retval['created'] = request.created
retval['request_id'] = request.request_id
retval['ready'] = request.is_ready()
if include_results:
translation_message = request.fetch_translation()
if type(translation_message) == TranslationRequestMessage:
retval['source_language'] = translation_message.source_language
retval['target_language'] = translation_message.target_language
retval['result'] = translation_message.target_text
retval.update([(x.key, x.value) for x in
translation_message.packet_data])
else:
retval['result'] = translation_message
return retval
Wiresharkを使用すると、read()はJSONを正しく返します。しかし
GET /mt-serverland/dashboard/api/results/?token=7594f0db HTTP/1.1
Host: localhost:8081
Accept-Encoding: identity
HTTP/1.0 200 OK
Date: Sat, 01 Jan 2011 01:05:36 GMT
Server: WSGIServer/0.1 Python/2.6.6
Vary: Authorization
Content-Type: application/json; charset=utf-8
[
{
"created": "2010-12-31 17:47:51",
"source_language": "eng",
"worker": "GoogleWorker",
"owner": "admin",
"result": "Esta es una prueba.\nEspero que este archivo se traducirá correctamente.",
"request_id": "b4ca75a301714a5097ce4daab35d370b",
"ready": true,
"shortname": "test01",
"target_language": "spa"
}
]
、私は()を作成使用しようと、私は次の応答を得る:私はちょうどRequestHandler.request_to_dict()
によって返されたオブジェクトを返すために)(作成、変更した場合
HTTP/1.0 201 CREATED Date: Thu, 30 Dec 2010 18:40:25 GMT Server: WSGIServer/0.1 Python/2.6.6 Vary: Authorization, Cookie Content-Type: text/plain Location: http://localhost:8081/mt-serverland/dashboard/api/requests/6614acd8491e4034bcb9b6a9430e6947/ Set-Cookie: sessionid=5536e58e88ded4365b536a354d3b8a7d; expires=Thu, 13-Jan-2011 18:40:25 GMT; Max-Age=1209600; Path=/ {'created': datetime.datetime(2010, 12, 30, 19, 40, 25, 282665), 'worker': u'GoogleWorker', 'ready': False, 'request_id': '6614acd8491e4034bcb9b6a9430e6947', 'owner': u'admin', 'shortname': u'1234567'}
を、そしてレスポンスの内容application/json
です。これがなぜ起こっているのでしょうか?ありがとう!
私は別のプロジェクトに取り組んでいるので、まだ試してみる機会はありませんでした。私はすぐに確認して確認します。ご回答有難うございます! –