我已经创建了一个过滤器api,其中包含多个参数,以便在iOS应用程序中传入url来过滤产品。api是在Django rest框架中用Python编写的。api在postman中运行良好,但是当我检查postman中Swift的代码时,它显示了一些复杂的url结构,我不知道如何将url字符串应用到我的Swift代码中。你知道吗
我的序列化程序类:
class ProductOptionsSerializer(serializers.ModelSerializer):
optionproductid = ProductSerializer()
optionattributeid = AttributeSerializer()
optionsid = serializers.IntegerField()
class Meta:
model = Productoptions
fields = ('optionproductid', 'optionattributeid', 'optionsid')
查看:
class GetProductByMulipleFilter(ListAPIView):
serializer_class = ProductOptionsSerializer
def get_queryset(self):
query_params = self.request.query_params
somethings = query_params.get('filters', None)
# create an empty list for parameters to be filters by
somethingParams = []
serializer = self.serializer_class()
# create the list based on the query parameters
if somethings is not None:
for something in somethings.split(','):
somethingParams.append(str(something))
if somethings is not None:
queryset_list = Productoptions.objects.all()
queryset_list = queryset_list.filter(optionattributeid__attributename__in=somethingParams)
return queryset_list
网址
url(r'^$', GetProductByMulipleFilter.as_view(), name='filters')
http://192.168.1.13:8000/api/?filters=puma,nike
它工作良好,显示出正确的结果。但swift中的代码显示的url如下:
let request = NSMutableURLRequest(url: NSURL(string: "http://192.168.1.13:8000/api/?filters=puma%2Cnike")! as URL
我的问题是为什么Swift代码中的url没有显示仅用“,”分隔的参数。你知道吗
你可以试试
因为请求在处理时将
,
符号编码为%2C
。你可以在Wiki上找到更多关于URL percent encoding
的信息相关问题 更多 >
编程相关推荐