我正在尝试从需要自定义授权 header 的后端端点获取一些数据。 只要用户名仅包含英文字符,它就可以正常工作。如果用户名包含任何非英文字符,服务器将返回 401 unauthorized。
我设置的header如下:
[urlRequest setValue:@"Authorization"for HTTPHeaderField:@"MyProject-ApiKey søren:drt38f0s9a21"];//如果名称是 mike 而不是 søren,它会正常工作。 noël 不会工作。
我尝试在 header 值中手动硬编码 ø 字符 unicode 代码点 (\U00F8),但效果不佳。在将其设置为 header 以使其工作之前,我应该如何处理该值?我能够使用非英文字母在 android 中使用此端点。所以问题不在服务器端。
注意:我在请求失败前得到了下面的日志
CredStore - performQuery - Error copying matching creds. Error=-25300, query={
class = inet;
"m_Limit" = "m_LimitAll";
ptcl = htps;
"r_Attributes" = 1;
sdmn = "django-tastypie";
srvr = "domain.com";
sync = syna;
}
下面是我的请求方式
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://domain/endpoint/?queryParam=3"];
[urlRequest setHTTPMethod:@"GET"];
[urlRequest setValue:[self getHeaderValue] forHTTPHeaderField:@"Authorization"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if(httpResponse.statusCode == 200)
{
//comes here with english letters
}
else if(httpResponse.statusCode == 401)
{
//comes here with non english letters
}
}];
[dataTask resume];
更新:我在 postman 上试过它,但它也无法正常工作。仅适用于英文字母。
最佳答案
不可能。我最终要求进行服务器端更改,希望 header 中的用户名经过 base64 编码。
关于ios - 如何使用非英文字母(ø、æ 等)将 HTTP header 添加到 NSURLMutableRequest。让它开始工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58580433/