-1

I am trying to establish a persistent connection between our node (express.js) server and a vendor server. We are currently using the "request" library, though we can easily move to something else. https://www.npmjs.com/package/request

The vendor has told us that the body of the Post request (to their API) needs to come before things like Content-Type, charset, etc. So like this:

"POST / HTTP/1.1\r\nREQUEST_JSON_API: **{\"REQUEST_TYPE\":\"STATUS\"}** \r\nContent-Type: application/json\r\ncharset: utf-8\r\nUser-Agent.."  

Instead of how it's currently coming across:

"POST / HTTP/1.1\r\nhost: xxx.x.xxx.1:3000\r\naccept: application/json\r\ncontent-type: application/json\r\ncontent-length: 25\r\nConnection: keep-alive\r\n\r\n **{\"REQUEST_TYPE\":\"STATUS\"}**"  

Does anyone have any advice on how to tackle this please?

Dee_wab
  • 1,171
  • 1
  • 10
  • 23
DanG
  • 91
  • 1
  • 9
  • I guess you just want to send it manually. As you wrote here. %)P – deEr. May 17 '18 at 04:45
  • Assuming that we don't have time to write our own library, is there anything you can recommend? – DanG May 17 '18 at 04:48
  • You don’t need a library. [Just use `net`.](https://nodejs.org/api/net.html#net_net_createconnection). – deEr. May 17 '18 at 04:53
  • 2
    That makes no sense that the body of a POST would come before the headers. That's not HTTP. Your first example is not showing that at all. It's showing a custom header named `REQUEST_JSON_API:` that has some JSON as the value of that header. It is not showing any body to the POST as the body comes after `\r\n\r\n` and you don't have one of those in your first example. – jfriend00 May 17 '18 at 04:58
  • 1
    You should be able to specify a custom header named `REQUEST_JSON_API: ` easily with the `request()` library. Did you try that? – jfriend00 May 17 '18 at 05:02
  • @jfriend00 thanks I will try. What I posted here was the result of their debug output. We don't have access to the server. For all I know this isn't even the problem and someone is just guessing that's what it is. Also yes if this is the problem, it isn't following the protocol as I understand it – DanG May 17 '18 at 05:06
  • 1
    It looks to me like you're misunderstanding what they want. You are attempting to put the data in the POST body, but apparently they want the data in a custom header. That's legal HTTP, just different than what you were trying to send it. You can do that just fine with any normal HTTP library such as the `request()` library. You just have to construct the proper custom HTTP header and specify that on the POST request. – jfriend00 May 17 '18 at 05:20
  • @jfriend00 thanks! I've never run into this before for json. I will do some research on what the advantages might be? I suppose there must have been a reason. Hopefully also this fixes the problem – DanG May 17 '18 at 05:38
  • 1
    No, I don't think it's a good design, just the way they did it so you have to create the request that way to get it to work. No need to research advantages. – jfriend00 May 17 '18 at 05:49
  • @jfriend00 thank you very much! We are now at least being heard by the server. Other obstacles remain, but this one is resolved. – DanG May 17 '18 at 07:19
  • @jfriend00 update for the sake of completing the loop: It turns out that they had attempted to create their own custom network stack. I was able to convince them to upgrade their c framework (which was so old it didn't support modern keepalive) and add a proper library which supports sockets. They had been trying to hack their way into a persistent connection. Weird stuff. – DanG Mar 17 '19 at 18:39

1 Answers1

2

I'll turn my comments into an answer since it apparently solved your original problem:

It looks to me like you're misunderstanding what they want. You are attempting to put the data in the POST body, but apparently they want the JSON data in a custom header called REQUEST_JSON_API with nothing in the POST body. That's different than what you were trying to send it.

You can do that just fine with any normal HTTP library such as the request() library. You just have to construct the proper custom HTTP header and specify that on the POST request.

You can see how to set custom headers with the request() library here.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 1
    I don't know why you were downvoted. I understand why I was as I was misunderstanding the problem. Anyways... It does seem to have worked for us? I think you correctly identified my misunderstanding and proposed a workable solution, in an admirably respectful and informative way. Thank you again! – DanG May 18 '18 at 01:00