POST the query and array

Hi, you need manually in C++ to make a post-request and send. It needs to include the array of numbers. Here's the problem:

int *binary = new int[bufferLength];

std::string data;
std::stringstream out(data);
out << "POST /push1_pub?id=game\r\n";
out << "Host: http://localhost\r\n";
out << "Content-Length: ";
out << bufferLength << "\r\n";
out << "\r\n";
out << reinterpret_cast<char*>(binary);

if (socket.send(data.c_str(), data.size()) == -1)
 std::cout << "Failed to send headers\n";
 // Get the response from the server

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

Tell me, please.
October 8th 19 at 00:12
5 answers
October 8th 19 at 00:14
First, this approach can be a problem with the encoding. Not any characters can be written in the body of the request, it all depends on Content-encoding. But I do not know, it is necessary to try.

The most reliable method now is to convert all numbers to strings, and these strings to write to the request, separating, for example, space. Don't know if this is the solution to your problem, but it will be easier to check the correctness of the request, and receive data on the server.

If you're doing as you try (write request the bytes representing the integers), then record to a string so you need:

out.write(reinterpret_cast<char*>(binary), bufferLength*sizeof(int));

Then it will capture the entire array as a whole. And you get that bytes are written until, until it encounters the null byte. And in the view int as zero bytes are common. In Content_length need to specify the number of bytes in the request body, i.e. the bufferLength*sizeof(int).
I do so because in the transmitted string, only numbers, on the client you have to change the byte order, but reading problems do not. Thanks for the idea. - Toy.Ziema commented on October 8th 19 at 00:17
October 8th 19 at 00:16
I'm a little do not know the advantages, but you are sure that the reinterpret_cast of the pointer to the array reinterpretive the entire array, not just the first element? And that again, the entire array is written to the stream?

Do std::cout << data; let's see what is written in the flow and with what the content-length
October 8th 19 at 00:18
Obviously, the length the Content-Length can not be set in some sort of abstract size of int-Oh, just like you. Is specified in bytes. Read the length of the output by sizeof(int) multiply, or something like that. And why the start-line is not standard, maybe that's why the Bad Request?
Not according to the standard — I mean, why method and uri only, where HTTP/version? - Toy.Ziema commented on October 8th 19 at 00:21
To the word Yes, Host, too, is incorrect. http:// it is not written. - colton commented on October 8th 19 at 00:24
Like it was possible not to ask finished just in case. By sizeof(int) is not necessary to multiply. In any case, as I understand it.
Just tried to pass an array to the stream, without a reinterpret_cast, a request is sent, everything is OK. But looking through the headers, I see:

Headers: POST /push1_pub?id=game HTTP/1.1<br> User-Agent: Lotosoft soccer client<br> Host: localhost<br> Accept: */*<br> Content-Type: application/octet-stream<br> Content-Length: 4<br> <br / > 0x165d790

I feel this is definitely not. I'm just passing a pointer to an array? - Ignacio_Romaguera commented on October 8th 19 at 00:27
If you have a int — a single character, it is not necessary, Yes. If you want Inty to pass as bitstream, I do not understand why it is not necessary. In General, just do not understand what and how you want to. What should it be? Character representation? raw date? If raw then what it is it you in the original array are in the array binary? - Toy.Ziema commented on October 8th 19 at 00:30
It is clear that it just received a pointer to an array, i.e. first element. Because int *binary is just a pointer, what makes you think that the stringstream it will deploy it in the view (and what, by the way?), you NDO? - colton commented on October 8th 19 at 00:33
October 8th 19 at 00:20
Here it is:
out << reinterpret_cast<char*>(binary);

Writes to the stream a pointer to a char*. Not an array. Pointer.
To recorded the array, we must loop to display the elements.
Not sure, but maybe you can:
out << std::string(reinterpret_cast<char*>(binary));
No, he's in the thread writes the zero-terminated string pointed to by char*. - Toy.Ziema commented on October 8th 19 at 00:23
Thank you. - colton commented on October 8th 19 at 00:26
October 8th 19 at 00:22
Show how, and most importantly, how do You fill the int array.

Find more questions by tags SocketsC++Computer networksWeb Development