Unicode C++
Unicode C++
X.com Create Post
See more X Examples
Causes the User to create a Post under the authorized account.Chilkat Unicode C++ Downloads
#include <CkHttpW.h>
#include <CkJsonObjectW.h>
#include <CkHttpResponseW.h>
#include <CkStringBuilderW.h>
void ChilkatSample(void)
{
bool success = false;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkHttpW http;
// Implements the following CURL command:
// curl -X POST "https://api.x.com/2/tweets" \
// -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
// -H "Content-Type: application/json" \
// -d '{"text":"Hello, X.com! This is my first post using the API."}'
// 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.
// {
// "text": "Hello, X.com! This is my first post using the API."
// }
CkJsonObjectW json;
json.UpdateString(L"text",L"Hello, X.com! This is my first post using the API.");
// Adds the "Authorization: Bearer YOUR_ACCESS_TOKEN" header.
// Get our access token.
CkJsonObjectW jsonToken;
success = jsonToken.LoadFile(L"qa_data/tokens/x.json");
if (success != true) {
wprintf(L"Failed to load x.json\n");
return;
}
// Adds the "Authorization: Bearer <token>" header.
http.put_AuthToken(jsonToken.stringOf(L"access_token"));
CkHttpResponseW resp;
success = http.HttpJson(L"POST",L"https://api.x.com/2/tweets",json,L"application/json",resp);
if (success == false) {
wprintf(L"%s\n",http.lastErrorText());
return;
}
CkStringBuilderW sbResponseBody;
resp.GetBodySb(sbResponseBody);
CkJsonObjectW jResp;
jResp.LoadSb(sbResponseBody);
jResp.put_EmitCompact(false);
wprintf(L"Response Body:\n");
wprintf(L"%s\n",jResp.emit());
int respStatusCode = resp.get_StatusCode();
wprintf(L"Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
wprintf(L"Response Header:\n");
wprintf(L"%s\n",resp.header());
wprintf(L"Failed.\n");
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "data": {
// "id": "1346889436626259968",
// "text": "Hello, X.com! This is my first post using the API."
// }
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
const wchar_t *Id = jResp.stringOf(L"data.id");
const wchar_t *Text = jResp.stringOf(L"data.text");
}