python restful HTTPサービスを実現

1585 ワード

このhttpサービスはflaskベースです
シンプルな環境:
pip install flask
pip install flask-restful

github:https://github.com/Addision/Restful-Server/tree/master/RestfulServer
 
c++は、単純なpostまたはgetリクエストを構築します.
std::string Http::CreateHttpHead(const std::string& method, http_map_type& book)
{
    std::string Header = "";
    Header.append(method+ " ");
    std::string book_id = book["id"];
    cout << book_id << endl;
    Header.append("/api/v1/books/"+book_id+" ");
    Header.append("HTTP/1.1\r
"); Header.append("Host: "); Header.append("192.168.199.111:5555\r
"); Header.append("Accept:*/*\r
"); Header.append("Content-Type: application/json;charset:UTF-8\r
"); Header.append("Cache-Control: no-cache\r
"); Header.append("Connection: close\r
"); if(method == "POST") { Json::Value root; Json::FastWriter writer; root["id"] = book_id; root["name"] = book["name"]; root["author"] = book["author"]; root["content"] = book["content"]; std::string str = writer.write(root); Header.append("Content-Length: "); Header.append(std::to_string(str.length())+"\r
\r
"); Header.append(str); } Header.append("\r
\r
"); cout << Header << endl; return Header; }