0

I am attempting to send a POST request to a server I have no control over. The server receives the POST but then sends a response every time an external event happens. So by "subscribing" with one POST I should be able to receive multiple responses until the connection is dropped. I have attempted to do this using sockets, but it seems the socket sends as TCP which the server does not recognise - it needs to be HTTP (I checked this using Wireshark as they have an APITool with which to test the connection).

This is my code to receive the data:

var net = require("net");
var client = new net.Socket();
client.connect(
  8080,
  "hostname.com",
  function() {
    console.log("Connected");
    client.write(`POST /Subscribe HTTP/1.1
Authorization: Basic authcode
Host: hostname.com:8080
Content-Length: 1078
Connection: keep-alive
Keep-Alive: 300

<?xml version="1.0" encoding="UTF-8"?>
<config version="1.7">
... (some XML here)
</config>


`);
    console.log("Finished Write");
  }
);

client.on("data", function(data) {
  console.log("Received " + data.length + " bytes\n" + data);
});

client.on("close", function() {
  console.log("Connection closed");
});

client.on("error", function(err) {
  console.log("Connection error" + err);
});

client.on("end", function(err) {
  console.log("Connection end" + err);
});

Is there a way to force the socket to send as HTTP, or is there a way to receive a "stream" from a POST request over http?

Thank you.

EDIT 1: Adding the raw HTTP code whePOST /SetSubscribe HTTP/1.1

Host: hostname.com:8080
Keep-Alive: 300
Authorization: Basic authkey
Content-Type: application/xml
Content-Length: 1076

<?xml version="1.0" encoding="UTF-8"?>
... XML
</config>

And this gives the response:

<?xml version="1.0" encoding="UTF-8" ?>
<config version="1.7" xmlns="http://www.ipc.com/ver10">
    ... XML
</config>

With the following headers:

Content-Type: application-xml
Content-Length: 397
Connection: keep-alive

When I watch this in WireShark I see multiple responses coming back but I don't know how to "grab" them in code to use them. Postman doesn't show each of these new responses coming back though.

Mudders
  • 127
  • 2
  • 12
  • Can you show how you send POST request and receive a response? – Anatoly Nov 11 '20 at 14:29
  • I can send as a standard POST in Postman, and it receives the first response, but then while watching WireShark I see the rest of the responses coming in, but I do not know how to keep open my connection to receive the rest of the responses. If that makes sense? – Mudders Nov 11 '20 at 14:38
  • Ok, show a raw response (first part and second part: only headers and a beginning of data – Anatoly Nov 11 '20 at 14:43
  • I've added what I send in Postman, and what comes back. As mentioned in my edit, WireShark shows multiple responses coming back but I don't know how to get them – Mudders Nov 11 '20 at 14:50
  • POST cannot have multiple responses. Only one response has to be sent. Maybe you're dealing with streamed response? – Anatoly Nov 11 '20 at 15:07
  • see https://stackoverflow.com/a/53231701/1376618 – Anatoly Nov 11 '20 at 15:07
  • That sounds like it - perhaps a streaming of data back to the client. Do you have any idea of how to setup a client to receive a streaming response? Or a link to get me on my way please? Much appreciated. Thanks – Mudders Nov 11 '20 at 15:19
  • Depends on how you send requests from the client now. Using something like `axios`? – Anatoly Nov 11 '20 at 15:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/224453/discussion-between-mudders-and-anatoly). – Mudders Nov 12 '20 at 05:47

0 Answers0