I'm trying to set the headers of an Alamofire 4 request where the headers have a key whose value is another key-value pair. The swift compiler is happy with my header variables but Alamofire won't take them.
This is what I have:
let subHeader = [
"some-key": "some-value",
"another-oey": "another-value"
]
let headers : [String: Any] = [
"some-header-key": "some-header-value",
"subdata": [subHeader]
]
// Upload data
Alamofire.upload(data,
to: uploadurl,
method: .put,
headers: headers)
.validate()
.response { response in
// Handle response
}
If you have a look at Alamofire's source, you can see HTTPHeaders
is actually [String: String]
. Basically, Alamofire
expects [String: String]
from you as headers
.
What you can do is converting your subheader
into String
with NSJSONSerialization
and pass it to Alamofire
.
let dictData = try! JSONSerialization.data(withJSONObject: subheader)
let dictString = String(data: dictData, encoding: String.Encoding.utf8)!
print(dictString)
// Now use "dictString" instead of "subheader" in your "headers" dictionary
You can do error handling however you want, I used !
s for simplicity.