How to see request and response data in node soap? - node.js

I am trying to consume a soap api using node soap. My response cannot be parsed and I wonder how to see the request and response data to console to ease the error finding process.

As node soap uses the request library, one can debug it via:
NODE_DEBUG=request node src/index.js
as pointed out request's Readme.md:
Debugging
There are at least three ways to debug the operation of request:
Launch the node process like NODE_DEBUG=request node script.js (lib,request,otherlib works too).
Set require('request').debug = true at any time (this does the same thing as #1).
Use the request-debug module to view request
and response headers and bodies.

To see the generated SOAP XML request you can use this:
Client.lastRequest - the property that contains last full soap request for client logging

Related

Why do we need to add .end() to a response?

Currently building a RESTful API with express on my web server, and some routes like the delete route for a document with mongoose ex. await Note.findByIdAndRemove(request.params.id) response.status(204).end() send response statuses with end()
Why do I need to add the .end()? What in these cases, and why cant one just send response.status(204)
With some responses that return json, the response.status(201).json works fine
Only certain methods with Express or the http interface will send the response. Some methods such as .status() or .append() or .cookie() only set state on the outgoing response that will be used when the response is actually sent - they don't actually send the response itself. So, when using those methods, you have to follow them with some method that actually sends the response such as .end().
In your specific example of:
response.status(204)
You can use the Express version that actually sends the response:
response.sendStatus(204)
If you choose to use .status() instead, then from the Express documentation, you have to follow it with some other method that causes the response to be sent. Here are examples from the Express documentation for .status():
res.status(403).end()
res.status(400).send('Bad Request')
res.status(404).sendFile('/absolute/path/to/404.png')
Since all three of these other methods will cause the response to be sent and when the response goes out, it will pick up the previously set status.

How to make post request using SOAP in node.js

I'm using the library SOAP (https://www.npmjs.com/package/soap) to make requests to a wsdl, but I'm getting the error HTTP GET not supported, using SOAPUI or even axios with post method seems to work

Possible to use http-parser-js in an Electron app?

I need to make an HTTP request to a service that returns malformed headers that the native Node.js parser can't handle. In a test script, I've found that I can use the http-parser-js library to make the same request and it handles the bad headers gracefully.
Now I need to make that work within the Electron app that needs to actually make the call and retrieve the data and it's failing with the same HPE_INVALID_HEADER_TOKEN. I assume, for that reason, that the native HTTP parser is not getting overridden.
In my electron app, I have the same code I used in my test script:
process.binding('http_parser').HTTPParser = require('http-parser-js').HTTPParser;
var http = require('http');
var req = http.request( ... )
Is there an alternate process binding syntax I can use within Electron?
This was not an electron issue. My app makes several different requests and most of the are to services that return proper headers. Originally, I was using the request-promise library to handle all calls, but I needed to modify the one call that returned bad headers.
The problem was that I was still using request-promise for the other calls and that library conflicts with the custom code I had to write to deal with the malformed headers. Once I modified my custom code to handle all requests, things worked much more smoothly.

Fetch post data after a request in NodeJS

i' m a bit new to Node, so question may be stupid...
I am sending a POST request to a website (through http.request) and I want to be able to use the invisible POST data I get along the response.
I hope this is achievable, and I think so since I am able to preview those data in Chrome debugger.
PS : I understand that we can use BodyParser to parse and get those while listening for a POST call server side, but I have found no example of how to use it coupled with an http.request.
Thanks !
If the body of the HTTP response contains JSON, then you need to parse it first in order to turn it from a string into a JavaScript object, like this:
var obj = JSON.parse(body);
console.log(obj.response.auth_token);
More info on various ways of sending a POST request to a server can be found here: How to make an HTTP POST request in node.js?
Edit : So we figured it out in the comments. The value I needed was in the form to begin with, as a hidden field. My error was to think it was generated afterward. So I'll just grab it first then login, so I can use it again for future POST requests on the website.

node.js parallel requests are crashing node app - can't render headers

Ways to replicate the problem,
A. continuously refresh the browser (cmd+r) and then the app crashes with the following message
B. run mocha test suite, inside I'm using npm module request for http request and all of them pass, however as soon as I send another request in parallel using postman while the tests are running, then the app crashes with the following message.
http.js:731
throw new Error('Can\'t render headers after they are sent to the client.'
^
Error: Can't render headers after they are sent to the client.
at ServerResponse.OutgoingMessage._renderHeaders (http.js:731:11)
at ServerResponse.writeHead (http.js:1152:20)
at puremvc.define.signInWithCredentialsSuccess (eval at include (/Library/WebServer/Documents/FICO/fico-node/app.js:3:32), <anonymous>:31:18)
the code referenced
signInWithCredentialsSuccess: function(request, response, authToken) {
response.writeHead(200, {'Content-Type': 'text/xml'})
response.end(this.mustache.render(this.successTemplate, {authToken: authToken}));
},
It's an express application combined with puremvc, so architecture is a bit different, for instance after app.get, the request and response are passed around to different decoupled modules and functions who accomplish their tasks and simply end the response by writing some output and then the process is repeated again for each subsequent request using new response/request objects passed.
I assume that I should be getting a new request and response object for each request but the error message confuses that for some reason the app.get is giving me a reference to an old response object which was already used to sent the information down to the client that's why it's refusing to modify the headers.
my node version is 0.10.32
please advise

Resources