邮递员Swift代码显示不同的网址从我的正常u

2024-09-28 01:25:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经创建了一个过滤器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没有显示仅用“,”分隔的参数。你知道吗


Tags: 代码selfnoneapiurlparamsqueryfilters
2条回答

你可以试试

let param = "anyThing"

let request = NSMutableURLRequest(url: NSURL(string: "http://192.168.1.13:8000/api/?filters=\(param)")! as URL

因为请求在处理时将,符号编码为%2C。你可以在Wiki上找到更多关于URL percent encoding的信息

When a character from the reserved set (a "reserved character") has special meaning (a "reserved purpose") in a certain context, and a URI scheme says that it is necessary to use that character for some other purpose, then the character must be percent-encoded. Percent-encoding a reserved character involves converting the character to its corresponding byte value in ASCII and then representing that value as a pair of hexadecimal digits. The digits, preceded by a percent sign (%) which is used as an escape character, are then used in the URI in place of the reserved character. (For a non-ASCII character, it is typically converted to its byte sequence in UTF-8, and then each byte value is represented as above.)

相关问题 更多 >

    热门问题