Chilkat Online Tools

unicodeC / Twitter API v2 / Bookmark a Tweet

Back to Collection Items

#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    HCkHttpW http;
    BOOL success;
    HCkJsonObjectW json;
    HCkHttpResponseW resp;

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

    http = CkHttpW_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.

    // {
    //   "tweet_id": "1503863301276827651"
    // }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"tweet_id",L"1503863301276827651");

    // Adds the "Authorization: Bearer <access_token>" header.
    CkHttpW_putAuthToken(http,L"<access_token>");

    resp = CkHttpW_PostJson3(http,L"https://api.twitter.com/2/users/:id/bookmarks",L"application/json",json);
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        return;
    }

    wprintf(L"%d\n",CkHttpResponseW_getStatusCode(resp));
    wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp));
    CkHttpResponseW_Dispose(resp);


    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);

    }

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-d '{
    "tweet_id": "1503863301276827651"
}'
https://api.twitter.com/2/users/:id/bookmarks

Postman Collection Item JSON

{
  "name": "Bookmark a Tweet",
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "exec": [
          "// This script extracts the ID of the authenticating user, so you do not have to pass it yourself.",
          "if (!pm.environment.get('access_token') || !pm.request.url.path.includes(':id')) {",
          "    return;",
          "}",
          "",
          "const [userId] = pm.environment.get('access_token').split('-');",
          "",
          "if (!userId) {",
          "    return;",
          "}",
          "",
          "const userIdIndex = pm.request.url.path.indexOf(':id');",
          "pm.request.url.path[userIdIndex] = userId;"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "auth": {
      "type": "oauth2",
      "oauth2": [
        {
          "key": "state",
          "value": "",
          "type": "string"
        },
        {
          "key": "scope",
          "value": "",
          "type": "string"
        },
        {
          "key": "accessTokenUrl",
          "value": "",
          "type": "string"
        },
        {
          "key": "authUrl",
          "value": "",
          "type": "string"
        },
        {
          "key": "tokenName",
          "value": "bookmarks",
          "type": "string"
        },
        {
          "key": "redirect_uri",
          "value": "",
          "type": "string"
        },
        {
          "key": "clientId",
          "value": "",
          "type": "string"
        },
        {
          "key": "grant_type",
          "value": "authorization_code_with_pkce",
          "type": "string"
        },
        {
          "key": "addTokenTo",
          "value": "header",
          "type": "string"
        }
      ]
    },
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"tweet_id\": \"1503863301276827651\"\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "https://api.twitter.com/2/users/:id/bookmarks",
      "protocol": "https",
      "host": [
        "api",
        "twitter",
        "com"
      ],
      "path": [
        "2",
        "users",
        ":id",
        "bookmarks"
      ],
      "variable": [
        {
          "key": "id",
          "value": "",
          "description": "The ID of the user who wishes to like a Tweet. The ID must belong to the authenticating user.\n"
        }
      ]
    },
    "description": "Allows an authenticated user to like a Tweet.\n\nFor full details, see the [API reference](https://developer.twitter.com/en/docs/twitter-api/tweets/bookmarks/api-reference/post-users-user_id-bookmarks) for this endpoint.\n\n[Sign up](https://t.co/signup) for the Twitter API"
  },
  "response": [
  ]
}