Path: blob/master/cpp/windows/ConsoleApplication2/ConsoleApplication2.cpp
644 views
#include "pch.h"12#define CURL_STATICLIB3#include "curl/curl.h"4#include <iostream>5#include <string>6#include "json/json/json.h"7#include "json/jsoncpp.cpp"8#include <fstream>91011using namespace std;1213namespace14{15size_t callback(16const char* in,17std::size_t size,18std::size_t num,19std::string* out)20{21const size_t totalBytes(size * num);22out->clear();23out->append(in, totalBytes);24return totalBytes;25}26}2728Json::Value sendRequest(string auth_token, string fileName, string mode) {2930curl_global_init(CURL_GLOBAL_ALL);313233curl_off_t speed_upload, total_time;34curl_mime *form = NULL;35curl_mimepart *field = NULL;3637CURL *hnd = curl_easy_init();3839//curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");40curl_easy_setopt(hnd, CURLOPT_URL, "https://api.platerecognizer.com/v1/plate-reader/");4142form = curl_mime_init(hnd); //initialize form fields4344/* Fill in the file upload field */45field = curl_mime_addpart(form);46curl_mime_name(field, "upload");47curl_mime_filedata(field, fileName.c_str());4849// Add more fields50// config mode51if(mode.length()) {52curl_mimepart *part = NULL;53part = curl_mime_addpart(form);54curl_mime_name(part, "config");5556if (strcmp(mode.c_str(),"redaction") == 0){57curl_mime_data(part, "{\"mode\":\"redaction\"}", CURL_ZERO_TERMINATED);58} else if (strcmp(mode.c_str(),"fast") == 0){59curl_mime_data(part, "{\"mode\":\"fast\"}", CURL_ZERO_TERMINATED);60} else{61cout << "Unknown config mode : "+mode+"\n Valid Options: fast, redaction\n";62exit(1);63}64}6566curl_easy_setopt(hnd, CURLOPT_MIMEPOST, form);6768struct curl_slist *headers = NULL;69headers = curl_slist_append(headers, "cache-control: no-cache");70headers = curl_slist_append(headers, ("Authorization: Token "+ auth_token).c_str());71curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);72unique_ptr<std::string> httpData(new std::string()); //initializing string pointer to get data73// Hook up data handling function.74curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, callback);7576// Hook up data container (will be passed as the last parameter to the77// callback handling function). Can be any pointer type, since it will78// internally be passed as a void pointer.79curl_easy_setopt(hnd, CURLOPT_WRITEDATA, httpData.get());8081CURLcode ret = curl_easy_perform(hnd); //perform the request8283if (ret != CURLE_OK) { //failed case84fprintf(stderr, "curl_easy_perform() failed: %s\n",85curl_easy_strerror(ret));86}87else {88/* now extract transfer info */89curl_easy_getinfo(hnd, CURLINFO_SPEED_UPLOAD_T, &speed_upload);90curl_easy_getinfo(hnd, CURLINFO_TOTAL_TIME_T, &total_time);9192fprintf(stderr, "Speed: %" CURL_FORMAT_CURL_OFF_T " bytes/sec during %"93CURL_FORMAT_CURL_OFF_T ".%06ld seconds\n",94speed_upload,95(total_time / 1000000), (long)(total_time % 1000000));96}9798curl_easy_cleanup(hnd);99curl_mime_free(form);100101Json::Value jsonData;102Json::Reader jsonReader;103104if (jsonReader.parse(*httpData, jsonData))105{106cout << jsonData;107}108else109{110std::cout << "Could not parse HTTP data as JSON" << std::endl;111std::cout << "HTTP data was:\n" << *httpData.get() << std::endl;112}113114return jsonData;115}116117118119int main(int argc, char *argv[])120{121string token = "MY_API_KEY";122Json::Value data;123if (argc >= 2) {124data = sendRequest(token, argv[1], argv[2]);125}126else {127cout << "Error:Invalid Arguments!\nUsage: program.exe <Image>\n program.exe <Image> <ConfigMode>";128}129130ofstream file;131file.open("responce.txt", ios::app);132file << data;133file << "\n\n";134file.close();135cout << "\n\n";136system("PAUSE");137return 0;138}139140141