iOS UrlSession.shared.dataTask移除utf-8“ +”字符并将其替换为“ " [英] iOS UrlSession.shared.dataTask removes utf-8 "+" character and replaces it with " "

查看:146
本文介绍了iOS UrlSession.shared.dataTask移除utf-8“ +”字符并将其替换为“ "的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用x-www-form-endoded数据创建对API的登录调用。我在邮递员中创建了一个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“ +”字符并将其替换为“ "的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆