Sample code for 30+ languages & platforms
Xbase++

Create a New GMail Label

See more GMail REST API Examples

Demonstrates how to create a new GMail label.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL cUserId
LOCAL oJson
LOCAL cUrl
LOCAL oResp

nSuccess := 0

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oHttp := CreateObject("Chilkat.Http")
oHttp:AuthToken := "GMAIL-ACCESS-TOKEN"

cUserId := "me"
oHttp:SetUrlVar("userId", cUserId)

//  Create the JSON to be sent in the HTTP request body.
//  The name of the new label is "questions".
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("name", "questions")
oJson:UpdateString("labelListVisibility", "labelShow")
oJson:UpdateString("messageListVisibility", "show")

oJson:EmitCompact := 0
? oJson:Emit()

//  The JSON contains this:
//  {
//    "name": "questions",
//    "labelListVisibility": "labelShow",
//    "messageListVisibility": "show"
//  }

cUrl := "https://www.googleapis.com/gmail/v1/users/{$userId}/labels"
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpJson("POST", cUrl, oJson, "application/json", oResp)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oJson:destroy()
    oResp:destroy()
    RETURN
ENDIF

? "status = " + Str(oResp:StatusCode)

//  A 200 response status indicate success.
IF (oResp:StatusCode != 200)
    ? oResp:BodyStr
    ? "Failed."
    oHttp:destroy()
    oJson:destroy()
    oResp:destroy()
    RETURN
ENDIF

//  A successful repsonse contains JSON that looks like this:

//  {
//   "id": "Label_43",
//   "name": "questions",
//   "messageListVisibility": "show",
//   "labelListVisibility": "labelShow"
//  }

? "response body:"
? oResp:BodyStr

? "GMail label created!"

oHttp:destroy()
oJson:destroy()
oResp:destroy()