![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) curl POST with JSON Input and JSON OutputSee more CURL ExamplesDemonstrates running a simple curl command with JSON input and JSON output.Note: This example requires Chilkat v11.5.0 or greater.
#include <C_CkStringBuilderW.h> #include <C_CkHttpCurlW.h> #include <C_CkJsonObjectW.h> void ChilkatSample(void) { BOOL success; HCkStringBuilderW sbTargetCurl; HCkHttpCurlW httpCurl; HCkJsonObjectW responseJson; int statusCode; const wchar_t *targetCurl; success = FALSE; // Run the following curl command // curl -X POST https://httpbin.org/post \ // -H "Content-Type: application/json" \ // -d '{ // "title": "foo", // "body": "bar", // "userId": 1 // }' // The backslashes at the end of lines are not required. Chilkat ignores them if present. sbTargetCurl = CkStringBuilderW_Create(); CkStringBuilderW_AppendLn(sbTargetCurl,L" curl -X POST https://httpbin.org/post \\"); CkStringBuilderW_AppendLn(sbTargetCurl,L" -H \"Content-Type: application/json\" \\"); CkStringBuilderW_AppendLn(sbTargetCurl,L" -d '{"); CkStringBuilderW_AppendLn(sbTargetCurl,L" \"title\": \"foo\","); CkStringBuilderW_AppendLn(sbTargetCurl,L" \"body\": \"bar\","); CkStringBuilderW_AppendLn(sbTargetCurl,L" \"userId\": 1"); CkStringBuilderW_AppendLn(sbTargetCurl,L" }'"); httpCurl = CkHttpCurlW_Create(); // Run the curl command. success = CkHttpCurlW_DoYourThing(httpCurl,CkStringBuilderW_getAsString(sbTargetCurl)); if (success == FALSE) { wprintf(L"%s\n",CkHttpCurlW_lastErrorText(httpCurl)); CkStringBuilderW_Dispose(sbTargetCurl); CkHttpCurlW_Dispose(httpCurl); return; } responseJson = CkJsonObjectW_Create(); CkJsonObjectW_putEmitCompact(responseJson,FALSE); CkHttpCurlW_GetResponseJson(httpCurl,responseJson); statusCode = CkHttpCurlW_getStatusCode(httpCurl); wprintf(L"response status code: %d\n",statusCode); wprintf(L"%s\n",CkJsonObjectW_emit(responseJson)); // Output: // response status code: 200 // { // "args": {}, // "data": "{\r\n \"title\": \"foo\",\r\n \"body\": \"bar\",\r\n \"userId\": 1\r\n }", // "files": {}, // "form": {}, // "headers": { // "Content-Length": "96", // "Content-Type": "application/json", // "Host": "httpbin.org", // "X-Amzn-Trace-Id": "Root=1-69e8db8b-459b3bdf7b7a3bc749184968" // }, // "json": { // "body": "bar", // "title": "foo", // "userId": 1 // }, // "origin": "123.222.222.222", // "url": "https://httpbin.org/post" // } // ---------------------------------------------------------------------------------- // Another example: // curl -X POST https://postman-echo.com/post \ // -H "Content-Type: application/json" \ // -d '{"foo":"bar"}' targetCurl = L"curl -X POST https://postman-echo.com/post -H \"Content-Type: application/json\" -d '{\"foo\":\"bar\"}'"; // Run the curl command. success = CkHttpCurlW_DoYourThing(httpCurl,targetCurl); if (success == FALSE) { wprintf(L"%s\n",CkHttpCurlW_lastErrorText(httpCurl)); CkStringBuilderW_Dispose(sbTargetCurl); CkHttpCurlW_Dispose(httpCurl); CkJsonObjectW_Dispose(responseJson); return; } CkHttpCurlW_GetResponseJson(httpCurl,responseJson); statusCode = CkHttpCurlW_getStatusCode(httpCurl); wprintf(L"response status code: %d\n",statusCode); wprintf(L"%s\n",CkJsonObjectW_emit(responseJson)); // Output: // response status code: 200 // { // "args": {}, // "data": { // "foo": "bar" // }, // "files": {}, // "form": {}, // "headers": { // "host": "postman-echo.com", // "content-length": "13", // "content-type": "application/json", // "x-forwarded-proto": "https", // "accept-encoding": "gzip, br" // }, // "json": { // "foo": "bar" // }, // "url": "https://postman-echo.com/post" // } CkStringBuilderW_Dispose(sbTargetCurl); CkHttpCurlW_Dispose(httpCurl); CkJsonObjectW_Dispose(responseJson); } |
||||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.