Swift
Swift
POST JSON to REST API with non-us-ascii Chars Escaped
See more REST Examples
Demonstrates how to POST to a REST API with non-usascii chars within JSON Unicode escaped.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
success = false
let rest = CkoRest()!
// Connect using TLS.
var bAutoReconnect: Bool = true
success = rest.connect(hostname: "chilkatsoft.com", port: 443, tls: true, autoReconnect: bAutoReconnect)
// Load JSON containing the following Korean text.
// {
// "BillAddr": {
// "Id": "239615",
// "Line1": "류리하",
// "Line2": "류리하류리하",
// "City": "류리하류리하",
// "Country": "US",
// "CountrySubDivisionCode": "AK",
// "PostalCode": "류리하"
// }
// }
let json = CkoJsonObject()!
json.emitCompact = false
success = json.loadFile(path: "qa_data/json/korean.json")
if success == false {
print("\(json.lastErrorText!)")
return
}
success = rest.addHeader(name: "Content-Type", value: "application/json; charset=UTF-8")
let sb = CkoStringBuilder()!
json.emitSb(sb: sb)
sb.encode(encoding: "unicodeescape", charset: "utf-8")
print("\(sb.getAsString()!)")
// The StringBuilder contains this:
// {
// "BillAddr": {
// "Id": "239615",
// "Line1": "\ub958\ub9ac\ud558",
// "Line2": "\ub958\ub9ac\ud558\ub958\ub9ac\ud558",
// "City": "\ub958\ub9ac\ud558\ub958\ub9ac\ud558",
// "Country": "US",
// "CountrySubDivisionCode": "AK",
// "PostalCode": "\ub958\ub9ac\ud558"
// }
// }
let sbResp = CkoStringBuilder()!
success = rest.fullRequestSb(httpVerb: "POST", uriPath: "/echo_request_body.asp", requestBody: sb, responseBody: sbResp)
if success == false {
print("\(rest.lastErrorText!)")
return
}
// Show the response.
print("Json Response: \(sbResp.getAsString()!)")
}