This topic was automatically generated from Slack. You can find the original thread here.
Sergio Rodriguez : Hi there, I am working with Mailchimp webhooks to create sources. I notice that the payload is delivered in application/x-www-form-urlencodedformat , and in Mailchimp’s documentation they suggest to use the body-parser npm library. However this is middleware that is attached to request handlers. So my question is, what is the best way to parse application/x-www-form-urlencoded incoming requests on sources? (the payload that is reflected in the run method). For instance, wonder if there is a way to attach body-parser to the underlying Pipedream component’s request mechanism, or if there is a recommended library, so far I have looked at URLSearchParams, but it looks like it will require more string manipulation.
Sergio Rodriguez : This is how body-parser is used in Mailchimp example, where it is attached to the express.js webserver to process incoming request:
> const express = require(“express”);
> const bodyParser = require(“body-parser”);
> // this is a stand-in for the code you’d use to write to your own database
> const fakeDB = require(“fakeDB”);
>
> const app = express();
>
> app.use(bodyParser.json())
> app.use(bodyParser.urlencoded({ extended: true }));
>
> app.post("/", (req, res) => {
> const { type, data } = req.body;
> if (type === “subscribe”) {
> fakeDB.subscribeUser(data);
> } else if (type === “unsubscribe”) {
> fakeDB.unsubscribeUser(data.id);
> }
> });
>
> app.listen(port, () =>
> console.log(Listening at http://localhost:3000)
> );
Dylan Sather (Pipedream) : Believe it or not, we should parse this and give you a friendly JavaScript object that contains the equivalent data. For example, if I send this to a source’s HTTP endpoint: