I send a POST request in my Swift app to my server, but the data received by the server is different from what I intended to send to the server.
The data I try to send is:
let parameters = PostData(
previousVisit : previousVisit,
lang : lang,
object : object,
enrolledSince : since
)
Which comes down in practice to:
PostData(previousVisit: "2024-03-20T10:33:48", lang: "NL", object: "{\"Location\":[\"19\"],\"Event\":[]}", enrolledSince: "{}")
But the server receives:
array(1) {
["{"lang":"NL","object":"{\"Location\":"]=>
array(1) {
["\"19\""]=>
string(0) ""
}
}
This is really strange. It seems that a part of the data is interpreted as key and a part as value and some data is lost.
This is the code I use to send it:
let url = URL(string: "MyPHPServer.com/script.php")!
let session = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = try! JSONEncoder().encode(parameters)
let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
etc.
Where did I fail?
I expect the same result on the server as in Swift. I tried to find out why it fails, by I couldn't find it.
Okay, I don't understand it, but I found a working solution.
This worked in the
session.DataTaskfor me.