我正在尝试使用boost.asio
(可能还有boost.beast
)向包含JavaScript的rest api发送一个包含JSON数据的POST
请求。当我使用Postman
时,api处理POST
请求并将其成功保存到mysql数据库中,但是当我尝试使用c++
代码发送该请求时,服务器崩溃并返回{{1 }}在c ++端,Exception: read_until: End of file
在服务器端。这是我用来发送请求的C ++代码...
Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1
}
下面是服务器代码的一部分,假定该代码可以处理发布请求并将其保存到mysql
#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
using namespace std;
int main(int argc, char* argv[])
{
cout << "main -start" << endl;
try
{
boost::asio::io_service io_service;
string ipAddress = argv[1]; //"localhost" for loop back or ip address otherwise, i.e.- www.boost.org;
string portNum = argv[2]; //"8000" for instance;
string hostAddress;
if (portNum.compare("80") != 0) // add the ":" only if the port number is not 80 (proprietary port number).
{
hostAddress = ipAddress + ":" + portNum;
}
else
{
hostAddress = ipAddress;
}
string wordToQuery = "";//this will be used for entry indexing
string queryStr = argv[3]; //"/api/v1/similar?word=" + wordToQuery;
string json = argv[4]; //json text
// Get a list of endpoints corresponding to the server name.
tcp::resolver resolver(io_service);
tcp::resolver::query query(ipAddress, portNum);
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
// Try each endpoint until we successfully establish a connection.
tcp::socket socket(io_service);
boost::asio::connect(socket, endpoint_iterator);
boost::asio::streambuf request;
std::ostream request_stream(&request);
request_stream << "POST " << queryStr << " HTTP/1.1\r\n"; // note that you can change it if you wish to HTTP/1.0
request_stream << "Host: " << hostAddress << "\r\n";
request_stream << "User-Agent: C/1.0";
request_stream << "Content-Type: application/json; charset=utf-8 \r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Content-Length: " << json.length() << "\r\n";
request_stream << "Connection: close\r\n\r\n";
request_stream << json;
// Send the request.
boost::asio::write(socket, request);
boost::asio::streambuf response;
boost::asio::read_until(socket, response, "\r\n");
// Check that response is OK.
std::istream response_stream(&response);
std::string http_version;
response_stream >> http_version;
unsigned int status_code;
response_stream >> status_code;
std::string status_message;
std::getline(response_stream, status_message);
if (!response_stream || http_version.substr(0, 5) != "HTTP/")
{
std::cout << "Invalid response\n";
return 1;
}
if (status_code != 200)
{
std::cout << "Response returned with status code " << status_code << "\n";
return 1;
}
// Read the response headers, which are terminated by a blank line.
boost::asio::read_until(socket, response, "\r\n\r\n");
// Process the response headers.
std::string header;
while (std::getline(response_stream, header) && header != "\r")
{
std::cout << header << "\n";
}
std::cout << "\n";
// Write whatever content we already have to output.
if (response.size() > 0)
{
std::cout << &response;
}
// Read until EOF, writing data to output as we go.
boost::system::error_code error;
while (boost::asio::read(socket, response,boost::asio::transfer_at_least(1), error))
{
std::cout << &response;
}
if (error != boost::asio::error::eof)
{
throw boost::system::system_error(error);
}
}
catch (std::exception& e)
{
std::cout << "Exception: " << e.what() << "\n";
}
return 0;
}
我正在使用app.post('/licence', function (req, res) {
collectRequestData(req, result => {
console.log(result);
sleep(5000);
connection.query('INSERT INTO licence SET ?',result, function (error, results, fields) {
if (error) throw error;
res.end(JSON.stringify(results));
});
});
});
function collectRequestData(request, callback) {
const FORM_URLENCODED = 'application/json';
if(request.headers['content-type'] === FORM_URLENCODED) {
let body = '';
request.on('data', chunk => {
body += chunk.toString();
});
request.on('end', () => {
callback(JSON.parse(body));
});
}
else {
callback(null);
}
进行编译,它工作正常,然后在运行g++ -I boost file.cpp -o test.o -lboost_thread -lboost_system -lboost_regex -lpthread
时出现错误。我的JSON_text看起来像这样./test.o host port route JSON_text
。如果有人对我在做什么错有任何见解,将不胜感激。我正在为服务器使用ubuntu 18.04和nodejs。