![]() |
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
(Node.js) 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.
var os = require('os'); if (os.platform() == 'win32') { var chilkat = require('@chilkat/ck-node23-win64'); } else if (os.platform() == 'linux') { if (os.arch() == 'arm') { var chilkat = require('@chilkat/ck-node23-linux-arm'); } else if (os.arch() == 'arm64') { var chilkat = require('@chilkat/ck-node23-linux-arm64'); } else { var chilkat = require('@chilkat/ck-node23-linux-x64'); } } else if (os.platform() == 'darwin') { var chilkat = require('@chilkat/ck-node23-mac-universal'); } function chilkatExample() { var 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. var sbTargetCurl = new chilkat.StringBuilder(); sbTargetCurl.AppendLn(" curl -X POST https://httpbin.org/post \\"); sbTargetCurl.AppendLn(" -H \"Content-Type: application/json\" \\"); sbTargetCurl.AppendLn(" -d '{"); sbTargetCurl.AppendLn(" \"title\": \"foo\","); sbTargetCurl.AppendLn(" \"body\": \"bar\","); sbTargetCurl.AppendLn(" \"userId\": 1"); sbTargetCurl.AppendLn(" }'"); var httpCurl = new chilkat.HttpCurl(); // Run the curl command. success = httpCurl.DoYourThing(sbTargetCurl.GetAsString()); if (success == false) { console.log(httpCurl.LastErrorText); return; } var responseJson = new chilkat.JsonObject(); responseJson.EmitCompact = false; httpCurl.GetResponseJson(responseJson); var statusCode = httpCurl.StatusCode; console.log("response status code: " + statusCode); console.log(responseJson.Emit()); // 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"}' var targetCurl = "curl -X POST https://postman-echo.com/post -H \"Content-Type: application/json\" -d '{\"foo\":\"bar\"}'"; // Run the curl command. success = httpCurl.DoYourThing(targetCurl); if (success == false) { console.log(httpCurl.LastErrorText); return; } httpCurl.GetResponseJson(responseJson); statusCode = httpCurl.StatusCode; console.log("response status code: " + statusCode); console.log(responseJson.Emit()); // 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" // } } chilkatExample(); |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.