如何阻止MOYA / Alamfire转义我的身体json参数?

时间:2018-07-12 15:09:50

标签: swift alamofire moya

我正在将Moya与Alamofire一起使用。它们紧密集成。但是,Moya或Alamofire都在POST请求中转义了我的身体json参数。

我正在寻找防止这种逃逸发生的地点和方式。

示例:

{ 
  "credentials": { 
    "secret": 
jJWoBkc64O0VzcjRQv4MIuQv0HgRLiNQZL7GAhtINz6EKuaK+u+YkBMi9z3v6rqLBh8TD8lIO3F+5t7iJB/FJw==

查看逃脱到/FJw==的{​​{1}}

1 个答案:

答案 0 :(得分:1)

不幸的是,这个仇恨现在对我有用:

struct JSONStringArrayEncoding: ParameterEncoding {

func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
    var urlRequest = urlRequest.urlRequest

    let data = try! JSONSerialization.data(withJSONObject: parameters!, options: JSONSerialization.WritingOptions.prettyPrinted)

    let json = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
    if let json = json {
        print(json)
        let newJson = json.replacingOccurrences(of: "\\/", with: "/")
        urlRequest?.httpBody = newJson.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue));
    }

    return urlRequest!
}   
}

然后参加任务

    var task: Task {
    switch self {
             case
         .postConnectAddress:
        return .requestParameters(parameters: parameters! , encoding: JSONStringArrayEncoding())