简体   繁体   English

Poco :: Net :: HTTPClientSession json数据未收到Content-Type

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

So I am developing a server side Nodejs/expressjs app and a clientside c++/Poco app. 所以我正在开发一个服务器端Nodejs / expressjs应用程序和一个客户端c ++ / Poco应用程序。 I've managed to create a session between where the server is hosted and client. 我已经设法在托管服务器和客户端之间创建一个会话。 However, any time i try to send my JSON payload, express.js shows req.body as empty. 但是,无论何时我尝试发送我的JSON有效负载,express.js都会将req.body显示为空。

Google didn't reveal much besides that Content-Type was likely not being transmitted correctly and it appears so. 谷歌没有透露太多内容,因为内容类型可能没有正确传输,看起来如此。 I do set it explicitly but apparently i'm missing a step. 我明确地设定了它,但显然我错过了一步。

client-side 客户端

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
}

server: 服务器:

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);
    });

});

It appears that this is on Poco's end because I can transmit test data from postman and it reports just fine. 看起来这是在Poco的结束,因为我可以从邮递员传输测试数据,它报告很好。 I also setKeepAlive to true on Poco and that appears to be ignored as well. 我也在Poco上将KeeAlive设置为true,这似乎也被忽略了。 Has anyone used Poco enough to help? 有人用Poco足以帮忙吗?

Got a little confused by the stateful stream style of communication. 对有状态的流媒体风格感到有些困惑。 It is http and technically still a stateless connection. 这是http,技术上仍然是无状态连接。 All of the information about the request, EXCEPT THE BODY, must be done before you send the initial request. 在发送初始请求之前,必须先完成有关请求的所有信息,除了身体。

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;

Also, needed to set the contentLength which I'd tried before but wasn't working due to the content-type not being sent properly. 此外,需要设置之前我尝试但由于内容类型未正确发送而无法工作的contentLength。 After the content length and type were set right, the server received correctly without a hitch. 内容长度和类型设置正确后,服务器正确接收正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Poco::Net::HTTPClientSession 在没有 unique_ptr 的情况下抛出异常 - Poco::Net::HTTPClientSession throw exception without unique_ptr Poco HTTPClientSession将标头添加到HTTPRequest - Poco HTTPClientSession adding headers to HTTPRequest 从Poco HTTPClientSession异步读取 - Async read from Poco HTTPClientSession 在 C++ 中使用 Poco::Net::HTTPClientSession 获取简单 GET 方法 REST API 的 BAD REQUEST 错误 - getting BAD REQUEST error for simple GET method REST API using Poco::Net::HTTPClientSession in C++ 如何将Poco :: Net :: HTTPClientSession的套接字设置为TCP_NODELAY? - How do I set a Poco::Net::HTTPClientSession's socket to TCP_NODELAY? 尽管正确设置了主机,方法和内容类型,但Poco库的PUT方法无法按预期工作 - Poco library PUT method not working as expected although host, method, content-type are set correctly 如何在Poco :: HTTPClientSession中获得下载大小? - How to get download size in Poco::HTTPClientSession? 如何设置.Net WebClient :: UploadString()的内容类型? - How to set content-type of .Net WebClient::UploadString()? Poco :: HttpClientSession.receiveResponse()抛出NoMessageException没有任何明显的原因 - Poco::HttpClientSession.receiveResponse() throws NoMessageException without any apparent reason Symbian获取内容类型? - Symbian get content-type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM