0
votes

I am using Moya with Alamofire. They are tightly integrated. However, either Moya or Alamofire is escaping my body json parameters in the POST request.

I am looking for where and how to prevent this escaping from happening.

Example:

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

See the /FJw== that gets escaped to \/FJw==

1
"is escaping my body json parameters" Could you be more explicit about that? Do you have a quick example? Escaping, you mean not putting your param in the request body? Or adding percent escape characters? - Larme
Your json example is broken, but I guess you omitted double-quotes. And inside JSON strings \/ is a valid escape sequence. You have no need to prevent it. - OOPer
it was a shortened version to show the issue. I need to remove the escape because it interferes with the REST service I am using - jimijon
If the REST service does not work for a valid JSON, the it is broken, which is incredible. Or you made it yourself? Why do you think \/ is the reason your REST service is interfered? Please show enough info. - OOPer

1 Answers

1
votes

Unfortunately this kludge worked for me for now:

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!
}   
}

then in task

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