Chilkat Online Tools

C / Trader v2 API / Update Watchlist

Back to Collection Items

#include <C_CkHttp.h>
#include <C_CkJsonObject.h>
#include <C_CkStringBuilder.h>
#include <C_CkHttpResponse.h>

void ChilkatSample(void)
    {
    HCkHttp http;
    BOOL success;
    HCkJsonObject json;
    HCkStringBuilder sbRequestBody;
    HCkHttpResponse resp;

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

    http = CkHttp_Create();

    // Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "name": "my first watchlist",
    //   "symbol": [
    //     "GOOG",
    //     "TSLA",
    //     "FB",
    //     "MSFT"
    //   ]
    // }

    json = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"name","my first watchlist");
    CkJsonObject_UpdateString(json,"symbol[0]","GOOG");
    CkJsonObject_UpdateString(json,"symbol[1]","TSLA");
    CkJsonObject_UpdateString(json,"symbol[2]","FB");
    CkJsonObject_UpdateString(json,"symbol[3]","MSFT");

    CkHttp_SetRequestHeader(http,"Apca-Api-Secret-Key","{{APCA_API_SECRET_KEY}}");
    CkHttp_SetRequestHeader(http,"Apca-Api-Key-Id","{{APCA_API_KEY_ID}}");

    sbRequestBody = CkStringBuilder_Create();
    CkJsonObject_EmitSb(json,sbRequestBody);

    resp = CkHttp_PTextSb(http,"PUT","https://domain.com/v2/watchlists/:watchlist_id",sbRequestBody,"utf-8","application/json",FALSE,FALSE);
    if (CkHttp_getLastMethodSuccess(http) == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(json);
        CkStringBuilder_Dispose(sbRequestBody);
        return;
    }

    printf("%d\n",CkHttpResponse_getStatusCode(resp));
    printf("%s\n",CkHttpResponse_bodyStr(resp));
    CkHttpResponse_Dispose(resp);


    CkHttp_Dispose(http);
    CkJsonObject_Dispose(json);
    CkStringBuilder_Dispose(sbRequestBody);

    }

Curl Command

curl -X PUT
	-H "Apca-Api-Key-Id: {{APCA_API_KEY_ID}}"
	-H "Apca-Api-Secret-Key: {{APCA_API_SECRET_KEY}}"
	-d '{
  "name": "my first watchlist",
  "symbol": ["GOOG","TSLA","FB","MSFT"]
}'
https://domain.com/v2/watchlists/:watchlist_id

Postman Collection Item JSON

{
  "name": "Update Watchlist",
  "request": {
    "method": "PUT",
    "header": [
      {
        "key": "Apca-Api-Key-Id",
        "value": "{{APCA_API_KEY_ID}}",
        "type": "text"
      },
      {
        "key": "Apca-Api-Secret-Key",
        "value": "{{APCA_API_SECRET_KEY}}",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"name\": \"my first watchlist\",\n  \"symbol\": [\"GOOG\",\"TSLA\",\"FB\",\"MSFT\"]\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{HOST}}/v2/watchlists/:watchlist_id",
      "host": [
        "{{HOST}}"
      ],
      "path": [
        "v2",
        "watchlists",
        ":watchlist_id"
      ],
      "variable": [
        {
          "key": "watchlist_id",
          "value": null
        }
      ]
    }
  },
  "response": [
  ]
}