如何获取C++json帖子的json负载

How to get json payload for a C++ json post

本文关键字:json 负载 C++json 何获取 获取      更新时间:2023-10-16

我需要创建一个接受post数据的c++cgi应用程序。我将接受一个json对象。如何获取有效载荷?

我可以使用以下获取数据

int main() {
    bool DEBUG = true;
    cout << "content-type: text/html" << endl << endl;
    //WHAT GOES HERE FOR POST
    json=?????
    //THIS IS A GET
    query_string = getenv("QUERY_STRING");
}

如果方法类型为POST(您可能还想检查一下),则POST数据会写入stdin。因此,您可以使用这样的标准方法:

// Do not skip whitespace, more configuration may also be needed.
cin >> noskipws;
// Copy all data from cin, using iterators.
istream_iterator<char> begin(cin);
istream_iterator<char> end;
string json(begin, end);
// Use the JSON data somehow.
cout << "JSON was " << json << endl;

这将把cin中的所有数据读入json,直到EOF发生。

假设apache:

文档可在此处找到:

您会在底部附近找到它,但post数据是通过stdin提供的。

#include <iostream>
#include <string>
#include <sstream>
int main() 
{   
    bool DEBUG = true;
    std::cout << "content-type: text/htmlnn"; // prefer nn to std::endl
                                                // you probably don't want to flush immediately.
    std::stringstream post;
    post << std::cin.rdbuf();
    std::cout << "Got: " << post.str() << "n";
}