0

我需要创建一个接受发布数据的 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");

}
4

2 回答 2

2

如果方法类型是POST(您可能还想检查一下),那么 POST 数据将写入标准输入。因此,您可以使用如下标准方法:

// 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。

于 2013-02-19T15:51:02.067 回答
2

假设阿帕奇:

文档可在此处找到

您会在底部附近找到它,但发布数据是通过标准输入提供的。

#include <iostream>
#include <string>
#include <sstream>

int main() 
{   
    bool DEBUG = true;

    std::cout << "content-type: text/html\n\n"; // prefer \n\n to std::endl
                                                // you probably don't want to flush immediately.

    std::stringstream post;
    post << std::cin.rdbuf();

    std::cout << "Got: " << post.str() << "\n";
}   
于 2013-02-19T15:54:51.113 回答