Swift
Swift
Create a New GMail Label
See more GMail REST API Examples
Demonstrates how to create a new GMail label.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = CkoHttp()!
http.authToken = "GMAIL-ACCESS-TOKEN"
var userId: String? = "me"
http.setUrlVar(name: "userId", value: userId)
// Create the JSON to be sent in the HTTP request body.
// The name of the new label is "questions".
let json = CkoJsonObject()!
json.updateString(jsonPath: "name", value: "questions")
json.updateString(jsonPath: "labelListVisibility", value: "labelShow")
json.updateString(jsonPath: "messageListVisibility", value: "show")
json.emitCompact = false
print("\(json.emit()!)")
// The JSON contains this:
// {
// "name": "questions",
// "labelListVisibility": "labelShow",
// "messageListVisibility": "show"
// }
var url: String? = "https://www.googleapis.com/gmail/v1/users/{$userId}/labels"
let resp = CkoHttpResponse()!
success = http.httpJson(verb: "POST", url: url, json: json, contentType: "application/json", response: resp)
if success == false {
print("\(http.lastErrorText!)")
return
}
print("status = \(resp.statusCode.intValue)")
// A 200 response status indicate success.
if resp.statusCode.intValue != 200 {
print("\(resp.bodyStr!)")
print("Failed.")
return
}
// A successful repsonse contains JSON that looks like this:
// {
// "id": "Label_43",
// "name": "questions",
// "messageListVisibility": "show",
// "labelListVisibility": "labelShow"
// }
print("response body:")
print("\(resp.bodyStr!)")
print("GMail label created!")
}