iOS UrlSession.shared.dataTask removes utf-8 quot;+quot; character and replaces it with quot; quot;(iOS UrlSession.shared.dataTask 删除 utf-8 “+字符并将其替换为 quot;)
问题描述
我正在使用 x-www-form-endoded 数据创建对 API 的登录调用.我在 Postman 中创建了一个 POST 并收到 200 响应.我使用 Postman 的导出功能为 Android 生成 OKHTTP 代码和为 iOS 生成 NSURL 代码.Android 代码工作正常,但 iOS 代码收到 401 响应.我使用 Charles Web 调试代理来查看实际为有效负载发送的内容.对于 android,用户名正确表示为username=james+jypsee@jypsee.com",但在 iOS 中显示为username=james jypsee@jypsee.com".我的 iOS 代码如下:
I'm creating a login call to an API using x-www-form-endoded data. I created a POST in Postman and received a 200 response. I used Postman's export function to generate the OKHTTP code for Android and NSURL code for iOS. The Android code works fine, but the iOS code receives a 401 response. I used Charles web debugging proxy to look at what was actually being sent for the payload. For android the username is correctly represented as "username=james+jypsee@jypsee.com" but in iOS it comes out as "username=james jypsee@jypsee.com". My iOS code is below:
let headers = [
"accept": "application/json",
"cache-control": "no-cache",
"content-type": "application/x-www-form-urlencoded"
]
let postData = NSMutableData(data: "username=james+jypsee@jypsee.com".data(using: .utf8)!)
postData.append("&password=redacted".data(using: .utf8)!)
postData.append("&grant_type=password".data(using: .utf8)!)
postData.append("&client_id=redacted".data(using: .utf8)!)
postData.append("&client_secret=redacted".data(using: .utf8)!)
let request = NSMutableURLRequest(url: NSURL(string: "https://redacted.com/api/oauth2/token/")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error)
} else {
let httpResponse = response as? HTTPURLResponse
print(httpResponse)
}
})
当我单步执行 iOS 代码时,NSMutableData 和 Data 对象都正确地将用户名显示为username=james+jypsee@jypsee.com",只是将 URLSession.shared 作为潜在的错误原因.但我不能进入 URLSession.shared.dataTask() 因为它是一个私有方法.任何帮助表示赞赏.
When I stepped through the iOS code both the NSMutableData and Data objects correctly displayed the username as "username=james+jypsee@jypsee.com", just leaving the URLSession.shared as the potential cause of error. But I can't step into URLSession.shared.dataTask() because its a private method. Any help is appreciated.
推荐答案
+"字符的编码存在一些问题.你可以试试这个:
There is some issue with encoding of "+" characters. You can try this instead:
let postData = NSMutableData(data: "username=james%2Bjypsee@jypsee.com".data(using: .utf8)!)
这篇关于iOS UrlSession.shared.dataTask 删除 utf-8 “+"字符并将其替换为 ""的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iOS UrlSession.shared.dataTask 删除 utf-8 “+"字符并将其替换为 ""
基础教程推荐
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
