node.jshttphttpsnpm-request

Need to specify order of HTTP request elements in Node.js


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?


Solution

  • 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.