=================================
Note: Using @Larme's trick to print out debugDescription of the request, and comparing with my working curl request, I was able to figure out the dumb bugs I made. 1. In the server request handler, I return a serializerError when something unrecognized, pretty confusing. 2. I made a stupid mistake in my request from swift, putting "GET_RECIPES" instead of "GET_RECIPE".
================================
I have a http service implemented with django rest framework, when I send requests via swift/alamofire, it cannot get the correct json response. However, requests sent via curl and postman get the correct json response. So I am confused what where is the issue, the django service side or swift request side?
- I have tried using .responseString instead of .responseJSON in swift to print out the resposne, but still the data is not in the response, basically the error occurs when the request reaches the server side.
- From django server side, the error reads "TypeError: Object of type 'property' is not JSON serializable". OK it seems the issue is from django side...
- But from curl and postman, I can get the json response without an issue, with the response header containing "Content-Type": "application/json", and for the django side everything is also OK. Then does this mean the django server can handle json response and it should be the issue of the swift request??
Code in 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)
}
}
Code of the request handler
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)
Trace stack from the server side:
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