I'm using the Django REST Framework.
@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)
I am sending a POST request using the POSTMAN client where in form-data
the key
is colour
and the value
is red
. It works perfectly and I get the expected result. This is what print request.DATA
looks like:
<QueryDict: {u'colour': [u'red']}>
When I send an actual request from iOS, print request.DATA
looks as shown and my filter query fails.
{u'colour': u'red'}
Here is how I construct the HTTP Post Request in iOS:
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"];
What can I do to make this work?
With POSTMAN you're sending form data and with iOS you send json data instead. That's why it is parsed differently. You need to set Content-Type: application/json
header on POSTMAN and send raw data:
{"color": "black"}
This way you will get the same dict format on both.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.