================================
注意:使用@Larme的技巧打印出请求的debugDescription,并将其与工作的curl请求进行比较,从而能够弄清我的愚蠢错误。 1.在服务器请求处理程序中,当一些无法识别的东西引起混乱时,我返回了serializerError。 2.我在迅速提出的请求中犯了一个愚蠢的错误,将“ GET_RECIPES”替换为“ GET_RECIPE”。
===============================
我有一个使用django rest框架实现的http服务,当我通过swift / alamofire发送请求时,它无法获得正确的json响应。但是,通过curl和postman发送的请求将获得正确的json响应。 所以我很困惑django服务端或swift请求端是什么问题?
快速输入代码
let parameters: [String: Any] = [
"type": "GET_RECIPE",
"details": ["ingredients" : ["egg", "bread"]]
]
let headers = ["Content-Type": "application/json"]
Alamofire.request(url, mothod: .post, parameters: parameters,
headers: headers, encoding: JSONEncoding.default)
.responseJSON {response in
if let data = response.result.value {
print(data)
}
}
请求处理程序的代码
class RecipesSerilizer(serializers.ModelSerializer):
class Meta:
model = Recipes
fields = ('id', 'url', 'author', 'category', 'title', 'description',
'instructions', 'tip', 'raw', 'score')
def get_recipes_given_ingredients(data):
logger.info('Get recipes for {}'.format(data.get('details')))
details = data.get('details')
ingredients = details.get('ingredients')
logger.info('GET_RECIPE for ingredients {}'.format(ingredients))
recipes = queries.get_recipe_recommendation_given_ingredients(ingredients)
serializer = RecipesSerilizer(recipes, many=True)
return Response(serializer.data)
服务器端的跟踪堆栈:
Internal Server Error: /get-recipes/
Traceback (most recent call last):
File "C:\Users\Yuanjun\Anaconda2\envs\online_bid\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\Yuanjun\Anaconda2\envs\online_bid\lib\site-packages\django\core\handlers\base.py", line 145, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Yuanjun\Anaconda2\envs\online_bid\lib\site-packages\django\core\handlers\base.py", line 143, in _get_response
response = response.render()
File "C:\Users\Yuanjun\Anaconda2\envs\online_bid\lib\site-packages\django\template\response.py", line 106, in render
self.content = self.rendered_content
File "C:\Users\Yuanjun\Anaconda2\envs\online_bid\lib\site-packages\rest_framework\response.py", line 72, in rendered_content
ret = renderer.render(self.data, accepted_media_type, context)
File "C:\Users\Yuanjun\Anaconda2\envs\online_bid\lib\site-packages\rest_framework\renderers.py", line 733, in render
context = self.get_context(data, accepted_media_type, renderer_context)
File "C:\Users\Yuanjun\Anaconda2\envs\online_bid\lib\site-packages\rest_framework\renderers.py", line 688, in get_context
'content': self.get_content(renderer, data, accepted_media_type, renderer_context),
File "C:\Users\Yuanjun\Anaconda2\envs\online_bid\lib\site-packages\rest_framework\renderers.py", line 424, in get_content
content = renderer.render(data, accepted_media_type, renderer_context)
File "C:\Users\Yuanjun\Anaconda2\envs\online_bid\lib\site-packages\rest_framework\renderers.py", line 107, in render
allow_nan=not self.strict, separators=separators
File "C:\Users\Yuanjun\Anaconda2\envs\online_bid\lib\site-packages\rest_framework\utils\json.py", line 28, in dumps
return json.dumps(*args, **kwargs)
File "C:\Users\Yuanjun\Anaconda2\envs\online_bid\lib\json\__init__.py", line 238, in dumps
**kw).encode(obj)
File "C:\Users\Yuanjun\Anaconda2\envs\online_bid\lib\json\encoder.py", line 201, in encode
chunks = list(chunks)
File "C:\Users\Yuanjun\Anaconda2\envs\online_bid\lib\json\encoder.py", line 437, in _iterencode
o = _default(o)
File "C:\Users\Yuanjun\Anaconda2\envs\online_bid\lib\site-packages\rest_framework\utils\encoders.py", line 68, in default
return super(JSONEncoder, self).default(obj)
File "C:\Users\Yuanjun\Anaconda2\envs\online_bid\lib\json\encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'property' is not JSON serializable
[14/May/2019 08:29:32] "POST /get-recipes/ HTTP/1.1" 500 124585
答案 0 :(得分:0)
我认为您的问题是您正在尝试将帖子发送至get请求。
尝试如下更改alamofire请求:
let parameters: [String: Any] = [
"type": "GET_RECIPE",
"details": ["ingredients" : ["egg", "bread"]]
]
let headers = ["Content-Type": "application/json"]
Alamofire.request(url, mothod: .get, parameters: parameters,
headers: headers, encoding: JSONEncoding.default)
.responseJSON {response in
if let data = response.result.value {
print(data)
}
}
答案 1 :(得分:0)
服务器可能在处理您的请求时崩溃,或者找不到给定的URL(由于斜杠)。
如果服务器在text/html
模式下运行时崩溃了,通常返回DEBUG
。这样便可以通过堆栈跟踪很好地显示崩溃原因。
真的很难分辨您的情况。如果您提供了错误的堆栈跟踪记录,那就太好了。