Swift
Swift
MercadoLibre - Create Test User
See more MercadoLibre Examples
Crea un usuario de testChilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = CkoHttp()!
// First get our previously obtained OAuth2 access token.
let jsonToken = CkoJsonObject()!
success = jsonToken.loadFile(path: "qa_data/tokens/mercadolibre.json")
// Implements the following CURL command:
// curl -X POST -H "Content-Type: application/json" -d
// '{
// "site_id":"MLA"
// }'
// https://api.mercadolibre.com/users/test_user?access_token=MY_ACCESS_TOKEN
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "site_id": "MLA"
// }
let json = CkoJsonObject()!
json.updateString(jsonPath: "site_id", value: "MLA")
http.setRequestHeader(name: "Content-Type", value: "application/json")
http.setUrlVar(name: "access_token", value: jsonToken.string(of: "access_token"))
let resp = CkoHttpResponse()!
success = http.httpJson(verb: "POST", url: "https://api.mercadolibre.com/users/test_user?access_token={$access_token}", json: json, contentType: "application/json", response: resp)
if success == false {
print("\(http.lastErrorText!)")
return
}
let sbResponseBody = CkoStringBuilder()!
resp.getBodySb(sb: sbResponseBody)
let jResp = CkoJsonObject()!
jResp.loadSb(sb: sbResponseBody)
jResp.emitCompact = false
print("Response Body:")
print("\(jResp.emit()!)")
// A response status code of 201 indicates success.
var respStatusCode: Int = resp.statusCode.intValue
print("Response Status Code = \(respStatusCode)")
if respStatusCode >= 400 {
print("Response Header:")
print("\(resp.header!)")
print("Failed.")
return
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "id": 577815702,
// "nickname": "TETE5320415",
// "password": "qatest4890",
// "site_status": "active",
// "email": "test_user_80450945@testuser.com"
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
var id: Int = jResp.int(of: "id").intValue
var nickname: String? = jResp.string(of: "nickname")
var password: String? = jResp.string(of: "password")
var site_status: String? = jResp.string(of: "site_status")
}