Alamofire 5 Escaping Forward Slashes

Alamofire put request remove backslashes in request json string

It's doing this because you said it to do it, more precisely in this line

request = try JSONEncoding.default.encode(request)

you already had an json encoded data, when you do it again, you are treating this data as a simple string, and than json escapes the special characters.

If you remove that line it should work

You might have to set the content-type of the request also

request.setValue("application/json", forHTTPHeaderField: "Content-Type")

but it's not always required

JSON encoding with backslashes

// This Dropbox url is a link to your JSON
// I'm using NSData because testing in Playground
if let data = NSData(contentsOfURL: NSURL(string: "https://www.dropbox.com/s/9ycsy0pq2iwgy0e/test.json?dl=1")!) {

var error: NSError?
var response: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.allZeros, error: &error)
if let dict = response as? NSDictionary {
if let key = dict["d"] as? String {

let strData = key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
var error: NSError?
var response: AnyObject? = NSJSONSerialization.JSONObjectWithData(strData!, options: NSJSONReadingOptions.allZeros, error: &error)

if let decoded = response as? NSDictionary {
println(decoded["IsSuccess"]!) // => 1
}

}
}
}

I guess you have to decode twice: the wrapping object, and its content.

base64EncodedString returns undesired backslashes

I ended using the custom JSONEncoder posted by csim at this answer: Alamofire 5 Escaping Forward Slashes.

I had to create a new class, using the code provided by csim, and then the Alamofire request became:

Alamofire.request(finalURL,
method: method,
parameters: bodyParams,
encoding: JSONEncodingWithoutEscapingSlashes.default,
headers: ["Content-Type": "application/json"])
.responseJSON { response in
// process response
}

Sending complex parameter with Alamofire Swift3 gets Status Code 500 error

Use

    let ep: [String: Any] = [
"name" : "someone",
"phone" : "12345"
]

let uv: [String: Any] = [
"address" : "Sanchaung",
"phone" : ["123","1234"],
"emergency_phone" : [ep,ep]
]

let parameters = [
"update_values" : uv
]

instead of your code.

You can compare the results of the json serialization with:

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

print(String(data: data!, encoding: .utf8))

Parameter Keys Have Square Brackets Alamofire iOS

This is how I accomplished it via encoding the parameters:

// Remove square brackets for POST request
let parameterEncoding = ParameterEncoding.Custom { requestConvertible, parameters in
let (mutableRequest, error) = ParameterEncoding.URL.encode(requestConvertible, parameters: parameters)
let httpBody = NSString(data: mutableRequest.HTTPBody!, encoding: NSUTF8StringEncoding)!
mutableRequest.HTTPBody = httpBody.stringByReplacingOccurrencesOfString("%5B%5D=", withString: "=").dataUsingEncoding(NSUTF8StringEncoding)
return (mutableRequest, error)
}

Then add the encoding to your post:

Alamofire.request(.POST, "http://example.com", parameters: ["foo": ["bar1", "bar2"]], encoding: parameterEncoding)


Related Topics



Leave a reply



Submit