簡體   English   中英

Poco :: Net :: HTTPClientSession json數據未收到Content-Type

[英]Poco::Net::HTTPClientSession json data Content-Type not received

所以我正在開發一個服務器端Nodejs / expressjs應用程序和一個客戶端c ++ / Poco應用程序。 我已經設法在托管服務器和客戶端之間創建一個會話。 但是,無論何時我嘗試發送我的JSON有效負載,express.js都會將req.body顯示為空。

谷歌沒有透露太多內容,因為內容類型可能沒有正確傳輸,看起來如此。 我明確地設定了它,但顯然我錯過了一步。

客戶端

void upload(std::list<std::string>& args) {
    if (args.size() == 0 || args.front() == "--help") {
        help("upload");
        return;
    }

    std::string repo = args.front();
    args.pop_front();

    std::string name, language;
    auto depends = getDepends(name, language);

    // start making the poco json object here
    Poco::JSON::Object obj;
    obj.set("name", name);
    obj.set("url", repo);

    Poco::URI uri("http://url-of-my-server:50001/make_repo");
    std::string path(uri.getPathAndQuery());


    if (path.empty()) path = "/";

    HTTPClientSession session(uri.getHost(), uri.getPort());
    HTTPRequest request(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1);
    HTTPResponse response;

    std::ostream& o = session.sendRequest(request);

    std::cout << response.getStatus() << " " << response.getReason() << std::endl;

    session.setKeepAlive(true);
    request.setContentType("application/json");  // definately set Content-Type right?
    obj.stringify(std::cout);                    // can confirm it is spitting out the valid json here
    obj.stringify(o);                            // place the json in the request stream

    std::istream& s = session.receiveResponse(response);

    // do stuff with returned data
}

服務器:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');

var database = require('./database.js');  // one of my files
var connection = database.connection;
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

var port = 50001;   // explicitly set port because environment port kept forcing port 3000

// just a callback to make sure i'm connected to my sql server
connection.query('SELECT 1',function(err, rows) {
    if(err) {
        console.error("Could not connect to the database.");
    } else {
        console.log('connected to database: ' + connection.threadId);
    }

    app.get('/', function(req, res){
        res.send('hello world');
    });

    // this is the route I invoke, (and it is definately invoked)
    app.post('/make_repo', function(req, res, next) {

        console.log(req.headers); // this always returns '{ connection: 'Close', host: 'url-of-my-server:50001' }
        console.log(req.body); // this always returns '{}'

    });

    var listener = app.listen(port, function() {
        console.log("port: " + listener.address().port);
    });

});

看起來這是在Poco的結束,因為我可以從郵遞員傳輸測試數據,它報告很好。 我也在Poco上將KeeAlive設置為true,這似乎也被忽略了。 有人用Poco足以幫忙嗎?

對有狀態的流媒體風格感到有些困惑。 這是http,技術上仍然是無狀態連接。 在發送初始請求之前,必須先完成有關請求的所有信息,除了身體。

HTTPClientSession session(uri.getHost(), uri.getPort());
HTTPRequest request(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1);
HTTPResponse response;

std::stringstream ss;
obj.stringify(ss);
request.setKeepAlive(true);
request.setContentLength(ss.str().size());
request.setContentType("application/json");  // definately set Content-Type right?

std::ostream& o = session.sendRequest(request);
obj.stringify(o);             // can confirm it is spitting out the valid 

std::cout << response.getStatus() << " " << response.getReason() << std::endl;

此外,需要設置之前我嘗試但由於內容類型未正確發送而無法工作的contentLength。 內容長度和類型設置正確后,服務器正確接收正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM