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データは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;

これにより、EOFが発生するまで、cinからjsonにすべてのデータが読み込まれます。

于 2013-02-19T15:51:02.067 に答える
2

Apache の場合:

ドキュメントは次の場所にあります。

下部近くにありますが、投稿データは stdin を介して提供されます。

#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 に答える