Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) OpenAI (ChatGTP) Create ImageSee more OpenAI ChatGPT ExamplesShow how to send a POST to create an image. Gets the JSON response with URLs and then downloads images to files. For more information, see https://platform.openai.com/docs/api-reference/images/create
#include <C_CkHttpW.h> #include <C_CkJsonObjectW.h> #include <C_CkHttpResponseW.h> #include <C_CkStringBuilderW.h> void ChilkatSample(void) { HCkHttpW http; BOOL success; HCkJsonObjectW json; HCkHttpResponseW resp; HCkStringBuilderW sbResponseBody; HCkJsonObjectW jResp; int respStatusCode; const wchar_t *url; HCkStringBuilderW sbLocalFilePath; int created; int i; int count_i; // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. http = CkHttpW_Create(); // Implements the following CURL command: // curl https://api.openai.com/v1/images/generations \ // -H "Content-Type: application/json" \ // -H "Authorization: Bearer $OPENAI_API_KEY" \ // -d '{ // "prompt": "A cute baby sea otter", // "n": 2, // "size": "1024x1024" // }' // 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. // { // "prompt": "A cute baby sea otter", // "n": 2, // "size": "1024x1024" // } json = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(json,L"prompt",L"A cute baby sea otter"); CkJsonObjectW_UpdateInt(json,L"n",2); CkJsonObjectW_UpdateString(json,L"size",L"1024x1024"); // Adds the "Authorization: Bearer $OPENAI_API_KEY" header. // This is NOT a real key. Change the "sk-vi...." to your own key. CkHttpW_putAuthToken(http,L"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"); CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json"); resp = CkHttpW_PostJson3(http,L"https://api.openai.com/v1/images/generations",L"application/json",json); if (CkHttpW_getLastMethodSuccess(http) == FALSE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); return; } sbResponseBody = CkStringBuilderW_Create(); CkHttpResponseW_GetBodySb(resp,sbResponseBody); jResp = CkJsonObjectW_Create(); CkJsonObjectW_LoadSb(jResp,sbResponseBody); CkJsonObjectW_putEmitCompact(jResp,FALSE); wprintf(L"Response Body:\n"); wprintf(L"%s\n",CkJsonObjectW_emit(jResp)); respStatusCode = CkHttpResponseW_getStatusCode(resp); wprintf(L"Response Status Code = %d\n",respStatusCode); if (respStatusCode >= 400) { wprintf(L"Response Header:\n"); wprintf(L"%s\n",CkHttpResponseW_header(resp)); wprintf(L"Failed.\n"); CkHttpResponseW_Dispose(resp); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); return; } CkHttpResponseW_Dispose(resp); // Sample JSON response: // (Sample code for parsing the JSON response is shown below) // { // "created": 1589478378, // "data": [ // { // "url": "https://..." // }, // { // "url": "https://..." // } // ] // } // Sample code for parsing the JSON response... // Use the following online tool to generate parsing code from sample JSON: // Generate Parsing Code from JSON sbLocalFilePath = CkStringBuilderW_Create(); // Download each image.. CkHttpW_ClearHeaders(http); CkHttpW_putAuthToken(http,L""); created = CkJsonObjectW_IntOf(jResp,L"created"); i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"data"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); url = CkJsonObjectW_stringOf(jResp,L"data[i].url"); CkStringBuilderW_SetString(sbLocalFilePath,L"c:/aaworkarea/image_"); CkStringBuilderW_AppendInt(sbLocalFilePath,i + 1); CkStringBuilderW_Append(sbLocalFilePath,L".png"); success = CkHttpW_Download(http,url,CkStringBuilderW_getAsString(sbLocalFilePath)); if (success == FALSE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); CkStringBuilderW_Dispose(sbLocalFilePath); return; } i = i + 1; } CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); CkStringBuilderW_Dispose(sbLocalFilePath); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.