gpt4 book ai didi

python - Postman Swift 代码显示与我的正常 url 不同的 url

转载 作者:行者123 更新时间:2023-11-30 11:44:25 25 4
gpt4 key购买 nike

我创建了一个过滤器 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 中的代码显示的网址如下:

let request = NSMutableURLRequest(url: NSURL(string: "http://192.168.1.13:8000/api/?filters=puma%2Cnike")! as URL

我的问题是为什么 Swift 代码中的 url 不显示仅用“,”分隔的参数。

最佳答案

因为请求在处理时将 ​​, 符号编码为 %2C 。您可以在 Wiki 上找到有关 URL 百分比编码 的更多信息。

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.)

关于python - Postman Swift 代码显示与我的正常 url 不同的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48989295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com