Skip to content

Commit

Permalink
T 072733: Update multipart form request serializer (#165)
Browse files Browse the repository at this point in the history
- With the existing implementation we were having difficulties sending HTTP multipart requests to the RestAPI.
- The issue appeared to be the boundary value that is sent alongside these requests.
- The RestAPI endpoint in question (/Documents/LiabilityRelease?consumerId=[ID]) wasn't accepting the existing boundary value.
- In a multipart request, a boundary acts like a marker for each key/value pair in the request's form data.
  • Loading branch information
bconway99 authored Mar 22, 2022
1 parent 44611d6 commit b28cd97
Showing 1 changed file with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,19 @@ public final class MultipartFormRequestSerializer: HTTPRequestSerializer {
static private let CRLF = "\r\n"

private var formData: [FormPart] = []
private let contentBoundary = MultipartFormRequestSerializer.randomContentBoundary()

lazy var contentBoundary: String = {
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let lettersLength = UInt32(letters.count)

let randomCharacters = (0..<12).map { _ -> String in
let offset = Int(arc4random_uniform(lettersLength))
let characters = letters[letters.index(letters.startIndex, offsetBy: offset)]
return String(characters)
}

return randomCharacters.joined()
}()

private lazy var inlineContentBoundary: String = {
return "--\(contentBoundary)"
Expand Down Expand Up @@ -62,19 +74,6 @@ public final class MultipartFormRequestSerializer: HTTPRequestSerializer {
return mutableRequest
}

private static func randomContentBoundary() -> String {
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let lettersLength = UInt32(letters.count)

let randomCharacters = (0..<12).map { _ -> String in
let offset = Int(arc4random_uniform(lettersLength))
let characters = letters[letters.index(letters.startIndex, offsetBy: offset)]
return String(characters)
}

return "----------------------------\(randomCharacters.joined())"
}

private func encodedDataFrom(string: String) -> Data? {
return string.data(using: .utf8)
}
Expand Down

0 comments on commit b28cd97

Please sign in to comment.