Can't call http service with JSON as payload, always sends as EJSON

I am trying to call a second function from within another app service function. I do a findOneAndReplace in my current function and get the document back. By default, it is in EJSON. I then pass this as a param to my second function via context.functions.execute(‘second function’, doc). Inside of this second function I use the http service to send a POST request to an external service. No matter what I do, JSON.stringify(), parse, or any combination the payload I sent in the POST is ALWAYS in EJSON and causes errors in the downstream system since it has no idea what the types are.

Is it not possible to convert the EJSON to a pure JSON object? The docs mention using stringify, which yes if you log it out in the function the stringified result looks correct, but I cannot get it converted to a JSON object even running JSON.parse(JSON.stringify(doc)). ALWAYS EJSON.

I always have encodeBodyAsJSON set on the http POST

Are you able to log what is being received down stream?

I don’t think you would easily be able to convert things with just JSON stringify and parse… I think you’ll have to manually convert things if I had to best guess. This is one of the annoying things about query docs in these functions :frowning:

It should be that simple, from their docs on Atlas Functions

To return a value as standard JSON, call JSON.stringify() on the value and then return the stringified result

I can confirm that when stringified and logged out, the document has all types removed, but when I JSON.parse it and send the request, the payload is still in EJSON format

I figured out the issue, pretty ridiculous to be honest. First of all this is documented nowhere, I had to read the code and stumbled upon the comments within the code. This is the description of the encodeBodyAsJson param you can pass to the http client within the stitch function:

Sets whether or not the included body should be encoded as extended JSON when sent to the url in this request. Defaults to false.

As far as I can tell this is document nowhere this explanation. So if you thought a param called “Encode body as JSON” encoded the body as JSON you would be a fool. It encodes it as EJSON