By continuing from this question ,
I am trying to convert [String : Any]
into String
and then passing that String
into forHTTPHeaderField
Attempt 1: Without Pretty
let encoder = JSONEncoder()
if let json = try? encoder.encode(jsonDict) {
convertedString = String(data: json, encoding: .utf8)!
}
print("JsonStringFormat ", convertedString )
let url = NSURL(string: getMenuURL)
let request = NSMutableURLRequest(url: url! as URL)
request.setValue(convertedString, forHTTPHeaderField: "SessionInfo")
print("\nHEADer__reQQ__ ", request.allHTTPHeaderFields)
OUTPUT:
JsonStringFormat {"Token":"96FFC5B994514B3D","UICulture":"en-CA ","LanguageCode":"ENG","CompanyID":"QAP","IMEINo":"1jzBG3TSrMzj\/tKihlEv8g=="}
HEADer__reQQ__ ["SessionInfo": "{\"Token\":\"96FFC5B994514B3D\",\"LanguageCode\":\"ENG\",\"UICulture\":\"en-CA \",\"CompanyID\":\"QAP\",\"IMEINo\":\"1jzBG3TSrMzj\\/tKihlEv8g==\"}"]
Attempt 2: With .pretty printed
let encoder = JSONEncoder()
// ADDING PRETTY FORMAT
encoder.outputFormatting = .prettyPrinted
if let json = try? encoder.encode(jsonDict) {
convertedString = String(data: json, encoding: .utf8)!
}
print("PrettyJsonStringFormat ", convertedString )
let url = NSURL(string: getMenuURL)
let request = NSMutableURLRequest(url: url! as URL)
request.setValue(convertedString, forHTTPHeaderField: "SessionInfo")
print("\nPrettyHeader__ ", request.allHTTPHeaderFields)
OUTPUT:
PrettyJsonStringFormat {
"Token" : "70E277954143414A",
"UICulture" : "en-CA ",
"LanguageCode" : "ENG",
"CompanyID" : "QAP",
"IMEINo" : "1jzBG3TSrMzj\/tKihlEv8g=="
}
PrettyHeader__ [:]
If I go with Attempt 1, BackSlash \
is appending in that value. To avoid that I go with Attempt 2, [Pretty Printed]
.
I don't know why request.allHTTPHeaderFields
not having that added header values.
Kindly guide me.