spdy-http2 / node-spdy

SPDY server on Node.js
2.81k stars 196 forks source link

Failing to send HTTP/1.0 POST request #385

Open yashpatel20 opened 3 years ago

yashpatel20 commented 3 years ago

Failing to send a HTTP/1.0 post request. Getting the following error.

TypeError [ERR_INVALID_PROTOCOL]: Protocol "http:" not supported. Expected "https:"
    at new NodeError (node:internal/errors:329:5)
    at new ClientRequest (node:_http_client:168:11)
    at Object.request (node:http:52:10)
    at /Users/yash/elocity/utilityportal-backend/src/ocpp/command/CommandStateManager.ts:167:43
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:94:5) {
  code: 'ERR_INVALID_PROTOCOL'
}

Configuration of the agent and http request client:

const agentConfig = {
    host: domain,
    port: Number.isNaN(port) ? 80 : +port,
    spdy: {
        protocols: ["http/1.0"],
        ssl: false
    },
};
const agent = spdy.createAgent(agentConfig);
const httpOptions = {
    agent,
    method: "POST",
    path,
    headers: {
        "Content-Type": "application/xml",
        "Content-Length": Buffer.byteLength(client.lastRequest),
    },
    protocol: "http:",
    port: Number.isNaN(port) ? 80 : +port,
};
const post_req = http.request(httpOptions, function (res) {
    res.setEncoding("utf8");
    var result = "";
    res.on("data", function (chunk) {
        result += chunk;
    });
    res.on("end", function () {
        console.log(result);
    });
});
post_req.removeHeader("Transfer-Encoding");
post_req.on("error", function (err) {
    console.log(err);
});
post_req.write(client.lastRequest);
post_req.end();
})

I am not really familiar with making HTTP/1.0 request so any help regarding this would be greatly appreciated.

lamweili commented 1 year ago

Add the plain options.

const agentConfig = {
    host: domain,
    port: Number.isNaN(port) ? 80 : +port,
    spdy: {
        protocols: ["http/1.0"],
+               plain: true, 
        ssl: false
    },
};