Answer the question
In order to leave comments, you need to log in
POST request and array?
Hello, you need to manually compose a post-request in C++ and send it. It must include an array of numbers. Here is the problem:
int *binary = new int[bufferLength];<br>
...<br>
<br>
std::string data;<br>
std::stringstream out(data);<br>
out << "POST /push1_pub?id=game\r\n";<br>
out << "Host: http://localhost\r\n";<br>
out << "Content-Length: ";<br>
out << bufferLength << "\r\n";<br>
out << "\r\n";<br>
out << reinterpret_cast<char*>(binary);<br>
<br>
if (socket.send(data.c_str(), data.size()) == -1)<br>
{<br>
std::cout << "Failed to send headers\n";<br>
}<br>
else<br>
{<br>
// Получаю ответ от сервера<br>
}<br>
Answer the question
In order to leave comments, you need to log in
Firstly, with this approach, there may be problems with the encoding. Not any characters can be written in the request body, it all depends on the Content-encoding. But I don't know for sure, I'll have to try.
The most reliable way is to honestly convert all numbers to strings and write these strings into a query, separating them, for example, with a space. I don’t know if this solution suits your task, but it will be easier to both check the correctness of the request and receive data on the server.
If you do as you are trying (write bytes that represent integers into the request), then you need to write it to a string like this:
out.write(reinterpret_cast<char*>(binary), bufferLength*sizeof(int));
I don't know the pros a bit at all, but are you sure that reinterpret_cast of a pointer to an array reinterprets the entire array, not just the first element? And thus besides all array will be written down in a stream?
Make std::cout << data; see what was written to the stream and with what content-length
Obviously, the length in the Content-Length field cannot be set in any way in some abstract int sizes, like yours. Specified in bytes. Read the length of the output, multiply by sizeof(int) or something like that. And why is the start line not according to the standard, maybe that's why Bad Request?
This:
out << reinterpret_cast<char*>(binary);
out << std::string(reinterpret_cast<char*>(binary));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question