Just lately, I’ve began working by myself PET undertaking in iOS.
The duty that I’ve at hand is as follows:
- Serialize object into JSON utilizing JSONEncoder
- Ship that JSON to server (Spring Boot)
- Obtain the response from that server
- and many others. and many others.
I’m utilizing a normal JSONEncoder with couple of settings (see beneath):
JSON Encoding half
// Serializing it to the JSON
let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = .withoutEscapingSlashes
jsonEncoder.dataEncodingStrategy = .deferredToData
self.outSyncPackage = strive jsonEncoder.encode(outSyncPackageObj)
let jsonStringToBeDelivered = String(information: self.outSyncPackage!, encoding: .utf8)
As per screenshot beneath, you may see that there aren’t any points there:
Encoded JSON Textual content
Then, I’m making a URLRequest, to be able to ship that JSON to the server:
Preparation of the URLRequest object for execution
self.urlRequest?.httpMethod = "POST"
self.urlRequest?.setValue("software/json", forHTTPHeaderField: "settle for")
// self.urlRequest?.setValue("gzip", forHTTPHeaderField: "Content material-Encoding")
self.urlRequest?.setValue(self.sessionId, forHTTPHeaderField: "sessionId")
self.urlRequest?.setValue(String(self.lastSyncDate), forHTTPHeaderField: "lastSyncDateTime")
self.urlRequest?.setValue(self.purchaseKey, forHTTPHeaderField: "subCode")
// Physique
self.urlRequest?.httpBodyStream = InputStream(information: self.outSyncPackage!)
On the server facet I count on the identical JSON, with escape characters or not.
Nevertheless, upon execution of this routine I get rubbish on the server facet as enter JSON:
Final result of the UrlRequest on the server facet
Can anyone, please, clarify to me what’s it that I’m doing improper and why JSON is scrambled upon receipt?
Precise Outcome:
%7Bpercent22accountCurrencyEntitiespercent22percent3Apercent5Bpercent5Dpercent2Cpercent22transactionTemplateEntitiespercent22percent3Apercent5Bpercent5Dpercent2Cpercent22serverRequestedReSyncpercent22percent3Afalsepercent2Cpercent22currencyEntitiespercent22percent3Apercent5Bpercent5Dpercent2Cpercent22categoryEntitiespercent22percent3Apercent5Bpercent5Dpercent2Cpercent22webServerConfigurationEntitiespercent22percent3Apercent5Bpercent5Dpercent2Cpercent22transactionEntitiespercent22percent3Apercent5Bpercent5Dpercent2Cpercent22accountEntitiespercent22percent3Apercent5Bpercent5Dpercent7D=
Anticipated Outcome:
{"accountCurrencyEntities":[],"transactionTemplateEntities":[],"serverRequestedReSync":false,"currencyEntities":[],"categoryEntities":[],"webServerConfigurationEntities":[],"transactionEntities":[],"accountEntities":[]}
PS. I attempted numerous methods of decoding that resultant string, and I discovered, that the textual content is definitely ‘HTTP URLed’ one way or the other (see the Кодирование URL part on the screenshot):
Varied encodings, together with HTML Escape