我正在使用 Django REST 框架。
@api_view(['POST'])
def image_params(request, format=None):
if request.method == 'POST':
print request.DATA
size = request.POST.get('size')
colour = request.POST.get('colour')
get_clothes = Clothes.objects.filter( Q(clothescolour=colour) | Q(clothessize=size))
serializer = ClothesSerializer(get_clothes, many=True)
result = serializer.data
print result
return Response(result,status=status.HTTP_201_CREATED)
我正在使用is和is中form-data
的POSTMAN 客户端发送 POST 请求。它工作得很好,我得到了预期的结果。这看起来像:key
colour
value
red
print request.DATA
<QueryDict: {u'colour': [u'red']}>
当我从 iOS 发送实际请求时,print request.DATA
如图所示,我的过滤器查询失败。
{u'colour': u'red'}
这是我在 iOS 中构造 HTTP Post 请求的方式:
NSDictionary* requestData = [NSDictionary dictionaryWithObjectsAndKeys:
@"red",
@"colour",
nil];
NSError *postError;
NSData *postData = [NSJSONSerialization dataWithJSONObject:requestData options:NSJSONWritingPrettyPrinted error:&postError];
[postRequest setHTTPMethod:@"POST"];
[postRequest setHTTPBody:postData];
[postRequest setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
我能做些什么来完成这项工作?