在C ++中发出POST请求并接收JSON响应

时间:2017-09-04 09:14:42

标签: c++ curl libcurl jsoncpp

我有一个应用程序,我将我的OpenCV C ++代码的base64数据发送到AWS URL,它将查找图像中面部的年龄和性别,并以JSON格式发回结果。

我使用cURL库通过POST请求发送数据。这部分工作正常,我能够发送数据,每帧都正确保存图像。

为了读取JSON数据,我使用jsoncpp库来解析JSON数据。

我想将这两个部分集成在一个功能中。当我发送单个图像时,这也可以。当我在循环中发送多个图像并接收其JSON响应时,我遇到了问题。我每次都会收到第一个JSON响应。我已经通过生成随机数并在服务器端每次发送它作为JSON响应来测试这一点,当我在C ++代码中收到它时,我每次只获得第一个JSON响应,即第一个数字。但是当我尝试接收JSON数据而不发送POST数据并只发送GET请求时,我每次都会正确接收JSON数据。我集成的两个部分的代码在这里:

#include <iostream>
#include <string>
#include <memory>
#include <sstream>
#include <curl/curl.h>
#include <jsoncpp/json/json.h>
#include <stdio.h>
#include "opencv2/opencv.hpp"
#include "base64.h"

using namespace std;
using namespace cv;

/*Converts Mat to base64 string*/
string image_to_base64(Mat image)
{
    string base64Output;
    vector<uchar> vec_frame;

    vector<int> vecCompression_params;

    vecCompression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
    vecCompression_params.push_back(90);

    imencode(".jpg", image, vec_frame, vecCompression_params);

    uchar *enc_msg = new uchar[vec_frame.size()];

    for(int i=0; i < vec_frame.size(); i++) 
        enc_msg[i] = vec_frame[i];

    base64Output = base64_encode(enc_msg, vec_frame.size());

    std::replace(base64Output.begin(), base64Output.end(), '+', '-'); 
    std::replace(base64Output.begin(), base64Output.end(), '/', '_');
    std::replace(base64Output.begin(), base64Output.end(), '=', '*');

    return base64Output;
}

/**Sends the base64 string to AWS**/
struct WriteThis 
{
  const char *readptr;

  long long sizeleft;
};

static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
{
  struct WriteThis *pooh = (struct WriteThis *)userp;

  if(size*nmemb < 1)
    return 0;

  if(pooh->sizeleft) 
  {
    *(char *)ptr = pooh->readptr[0]; /* copy one single byte */ 
    pooh->readptr++;                 /* advance pointer */ 
    pooh->sizeleft--;                /* less data left */ 
    return 1;                        /* we return 1 byte at a time! */ 
  }

  return 0;                          /* no more data left to deliver */ 
}

string cURL_JSON_data;
size_t writeJSONURLCallback(char* buf, size_t size, size_t nmemb, void* up)//Callback function to store the URL's data
{ 
    for (int c = 0; c<size*nmemb; c++)
    {
        cURL_JSON_data.push_back(buf[c]);
    }
    return size*nmemb; 
}
string jsonTest;

tuple<string, string> send_base64(string base64_string)
{
    CURL *curl;
    CURLcode res;

    long long int httpCode(0);
    unique_ptr<std::string> httpData(new std::string());
    string age_info, gender_info;

    struct WriteThis pooh;
    //cout<<"base64_string.length(): "<<base64_string.length();

    char data[base64_string.length()];
    strcpy(data, base64_string.c_str());

    pooh.readptr = data;
    pooh.sizeleft = (long long)strlen(data);
    //cout<<"strlen(data): "<<strlen(data)<<endl;

    /* In windows, this will init the winsock stuff */ 
    res = curl_global_init(CURL_GLOBAL_DEFAULT);
    /* Check for errors */ 
    if(res != CURLE_OK) 
    {
        fprintf(stderr, "curl_global_init() failed: %s\n",
        curl_easy_strerror(res));
        //return 1;
    }

    /* get a curl handle */ 
    curl = curl_easy_init();
    if(curl) 
    {
        /* First set the URL that is about to receive our POST. */ 
        curl_easy_setopt(curl, CURLOPT_URL, "MYURL");

        /* Now specify we want to POST data */ 
        curl_easy_setopt(curl, CURLOPT_POST, 1L);

        /* we want to use our own read function */ 
        curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);

        /* pointer to pass to our read function */ 
        curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);

        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeJSONURLCallback);//writes the conents of url

        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); //output cURL's progress

        curl_easy_setopt(curl, CURLOPT_WRITEDATA, httpData.get());

        /* get verbose debug output please */ 
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

        /*
          If you use POST to a HTTP 1.1 server, you can send data without knowing
          the size before starting the POST if you use chunked encoding. You
          enable this by adding a header like "Transfer-Encoding: chunked" with
          CURLOPT_HTTPHEADER. With HTTP 1.0 or without chunked transfer, you must
          specify the size in the request.
        */ 
        #ifdef USE_CHUNKED
        {
            struct curl_slist *chunk = NULL;

            chunk = curl_slist_append(chunk, "Transfer-Encoding: chunked");
            res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
            /* use curl_slist_free_all() after the *perform() call to free this
            list again */ 
        }
        #else
        /* Set the expected POST size. If you want to POST large amounts of data,
           consider CURLOPT_POSTFIELDSIZE_LARGE */ 
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, (pooh.sizeleft));
        #endif

        #ifdef DISABLE_EXPECT
        /*
          Using POST with HTTP 1.1 implies the use of a "Expect: 100-continue"
          header.  You can disable this header with CURLOPT_HTTPHEADER as usual.
          NOTE: if you want chunked transfer too, you need to combine these two
          since you can only set one list of headers with CURLOPT_HTTPHEADER. */ 

        /* A less good option would be to enforce HTTP 1.0, but that might also
           have other implications. */ 
        {
          struct curl_slist *chunk = NULL;

          chunk = curl_slist_append(chunk, "Expect:");
          res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
          /* use curl_slist_free_all() after the *perform() call to free this
             list again */ 
        }
        #endif

        /* Perform the request, res will get the return code */ 
        res = curl_easy_perform(curl);
        /* Check for errors */ 
        if(res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));

        /* always cleanup */ 
        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
        //cout<<"Testing_1"<<endl;
        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();

    Json::Reader jsonReader;
    Json::Value jsonData;

    if (jsonReader.parse(cURL_JSON_data, jsonData))
    {   
        cout << "Successfully parsed JSON data" << std::endl;
        cout << "\nJSON data received:" << std::endl;
        jsonTest = jsonData.toStyledString();
        cout << jsonData.toStyledString() << std::endl;

        age_info = jsonData["Age"].asString();
        gender_info = jsonData["Gender"].asString();

    }
    else
    {
        cout<<"Could not parse JSON data"<<endl;
    }

    jsonData.clear();
    return make_tuple(age_info, gender_info);
}

int main()
{
    Mat frame;

    string base64_data,age, gender;
    int count = 0;

    VideoCapture cap(1);
    while(1)
    {
        cap>>frame;
        count++;
        resize(frame, frame, Size(frame.cols / 2, frame.rows / 2));
        if(count % 10 ==0)//Sending the data every 10 frames
        {
            base64_data = image_to_base64(frame);

            tie(age, gender) = send_base64(base64_data);    

            cout<<"age: "<<age<<", gender: "<<gender<<endl;
        }
        imshow("window", frame);
        waitKey(30);    
    }   
    return 0;
}

我用来接收只有GET请求的JSON的代码在这里:

#include <iostream>
#include <stdlib.h> 
#include <string>
#include <memory>
#include <curl/curl.h>
#include <jsoncpp/json/json.h>

using namespace std;

string cURLdata;

size_t writeURLCallback(char* buf, size_t size, size_t nmemb, void* up)
{ 
    for (int c = 0; c<size*nmemb; c++)
    {
        cURLdata.push_back(buf[c]);
    }
    return size*nmemb; 
}

int main()
{
    int count = 0;
    while(1){   
        CURL* curl; //curl object

        curl_global_init(CURL_GLOBAL_ALL); 
        curl = curl_easy_init();

        curl_easy_setopt(curl, CURLOPT_URL, "MYURL");
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeURLCallback);//writes the conents of url
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); //output cURL's progress

        curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        curl_global_cleanup();

        Json::Reader jsonReader;
        Json::Value jsonData;

        bool parsedSuccess = jsonReader.parse(cURLdata,jsonData,false);

        if (parsedSuccess)
        {
            std::cout << "Successfully parsed JSON data" << std::endl;
            std::cout << "\nJSON data received:" << std::endl;
            cout << jsonData.toStyledString() << std::endl;

        }
        else
        {
           std::cout << "Could not parse HTTP data as JSON" << std::endl;
        }
        jsonData.clear();
        cURLdata.clear();
    }
    return 0;
}

有人可以帮我指出我做错了吗?

0 个答案:

没有答案