O
O
Ockonal2012-04-15 07:57:27
C++ / C#
Ockonal, 2012-04-15 07:57:27

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>


I can't figure out how to correctly pass an array of int-elements, what size to specify in Content-Length. In the code I showed, the server is not responding, apparently waiting for more information. If you remove the cast to char *, then always Bad Request.

Tell me please.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
R
Riateche, 2012-04-15
@Ockonal

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

Then the entire array will be written. And it turns out that bytes are written until a null byte is encountered. And in the int representation, null bytes are common. In Content_length, you need to specify the number of bytes in the request body, i.e. bufferLength*sizeof(int).

M
Melkij, 2012-04-15
@melkij

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

B
barker, 2012-04-15
@barker

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?

M
Monnoroch, 2012-04-15
@Monnoroch

This:

out << reinterpret_cast<char*>(binary);

Writes a pointer to char* to the stream. Not an array. Pointer.
To write an array, you need to loop through the elements.
I'm not sure, but it could be like this:
out << std::string(reinterpret_cast<char*>(binary));

I
ixSci, 2012-04-15
@ixSci

Show how, and most importantly, how you fill an int array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question