Base64 safe url on Swift

Viewed 38

I'm trying to send a base64 string who is a sandbox-key, the problem is that Postman has encrypted different comparison with Swift. Here's the result...

originalString = "sk_289d8dd8e721484c9c8c63e39899cf90"

postmanBase64 = "c2tfMjg5ZDhkZDhlNzIxNDg0YzljOGM2M2UzOTg5OWNmOTA6"

swiftBase64 = "c2tfMjg5ZDhkZDhlNzIxNDg0YzljOGM2M2UzOTg5OWNmOTA="

Do you have any ideas to fix this?, why Postman encode different?

Swift Encoding:

 let authHeader = "sk_289d8dd8e721484c9c8c63e39899cf90".base64Encoded()!
            
 let authHeaderBase64 = toggleBase64URLSafe(item: authHeader, on: true)
            
  public extension String {
    func base64Encoded() -> String? {
        return data(using: .utf8)?.base64EncodedString()
    }
  }
  
    func toggleBase64URLSafe(item: String, on: Bool) -> String {
        if on {
            // Make base64 string safe for passing into URL query params
            let base64url = item.replacingOccurrences(of: "/", with: "_")
                .replacingOccurrences(of: "+", with: "-")
                .replacingOccurrences(of: "=", with: "")
            return base64url
        } else {
            // Return to base64 encoding
            var base64 = item.replacingOccurrences(of: "_", with: "/")
                .replacingOccurrences(of: "-", with: "+")
            // Add any necessary padding with `=`
            if base64.count % 4 != 0 {
                base64.append(String(repeating: "=", count: 4 - base64.count % 4))
            }
            return base64
        }
    }

ps. My request works if I use postmanBase64

This is my postman output to translate my request into swift code as you can see, this value has been translated automatically

enter image description here

0 Answers
Related