Unable to modify request body

Former Community Member
Not applicable

Hi, I am trying to write a custom plugin to expose a SOAP Web Service as REST API.

I read the docs and I managed to convert from json to correct SOAP request, but when it comes to send the request to the target service I get this error

{"message":"write after end","code":"ERR_STREAM_WRITE_AFTER_END"}

To be sure that it was not my plugin's fault, I tried with a fresh installation (I'm using latest version 3.1.4 on a docker container), with only one plugin activated: accumulate-request. I configured httpbin.org as target service, so I can inspect what flows out from MG.

When I send a POST request with no body, everything works as expected and httpbin respond correctly, but as I try to put something in the body I get the same error as above (here with the full stack trace):

2020-06-19T13:25:49.439Z gateway:main targetRequest error 64038cb0-b230-11ea-b7ce-4582586cfc80 Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at write_ (_http_outgoing.js:637:17)
at ClientRequest.write (_http_outgoing.js:629:15)
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/lib/plugins-middleware.js:434:35
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/node_modules/async/lib/async.js:1154:26
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/node_modules/async/lib/async.js:380:13
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/node_modules/async/lib/async.js:52:16
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/node_modules/async/lib/async.js:269:32
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/node_modules/async/lib/async.js:44:16
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/node_modules/async/lib/async.js:377:17
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/node_modules/async/lib/async.js:1150:21
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/node_modules/async/lib/async.js:167:37
at fx (/usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/lib/plugins-middleware.js:579:25)
at onend_request (/usr/local/lib/node_modules/edgemicro/node_modules/microgateway-plugins/accumulate-request/index.js:36:7)
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/lib/plugins-middleware.js:596:31
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/node_modules/async/lib/async.js:1149:20
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/node_modules/async/lib/async.js:375:13
2020-06-19T13:25:49.439Z gateway:main plugin analytics does not provide handler function for error_request
2020-06-19T13:25:49.439Z gateway:main plugin accumulate-request does not provide handler function for error_request
2020-06-19T13:25:49.440Z gateway:errors Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at write_ (_http_outgoing.js:637:17)
at ClientRequest.write (_http_outgoing.js:629:15)
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/lib/plugins-middleware.js:434:35
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/node_modules/async/lib/async.js:1154:26
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/node_modules/async/lib/async.js:380:13
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/node_modules/async/lib/async.js:52:16
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/node_modules/async/lib/async.js:269:32
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/node_modules/async/lib/async.js:44:16
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/node_modules/async/lib/async.js:377:17
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/node_modules/async/lib/async.js:1150:21
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/node_modules/async/lib/async.js:167:37
at fx (/usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/lib/plugins-middleware.js:579:25)
at onend_request (/usr/local/lib/node_modules/edgemicro/node_modules/microgateway-plugins/accumulate-request/index.js:36:7)
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/lib/plugins-middleware.js:596:31
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/node_modules/async/lib/async.js:1149:20
at /usr/local/lib/node_modules/edgemicro/node_modules/microgateway-core/node_modules/async/lib/async.js:375:13

The same thing happens with any plugins that send anything as body at the end of onend_request chain, like, for example, json2xml.

This seems like a bug, to me. Is there anybody able to use one of this plugin out of the box?

Any help is very appreciated.

Cheers

Lorenzo

0 1 379
1 REPLY 1

robinm
Participant IV

Howdy Lorenzo - did you come right?
If so, please share RCA ... otherwise one thing to consider is the execution order of events.

Where you have multiple plugins in a sequence, I have yet to be able to correctly implement async processing .... you may be able to confirm same by adding (console/log) displays in your plugins ... to check if the processing has actually continued to the next plugin .... before you get your (callout) response and try to write to the body.
I have lost many hours trying - so obviously I must be doing something wrong because it doesn't seem to work as advertised, LOL 🙂 .