How do I parse URL-encoded form data arriving sent to a Pipedream HTTP endpoint?

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)
> );

Sergio Rodriguez : On the other hand, parsing with UrlSearchParams yields this:

> [ ‘type’, ‘subscribe’ ]
> [ ‘fired_at’, ‘2021-05-05 21:15:37’ ]
> [ ‘data[id]’, ‘6260cf3471’ ]
> [ ‘data[email]’, ‘sergio_eliot@hotmail.com’ ]
> [ ‘data[email_type]’, ‘html’ ]
> [ ‘data[ip_opt]’, ‘187.172.106.101’ ]
> [ ‘data[web_id]’, ‘434574458’ ]
> [ ‘data[merges][EMAIL]’, ‘sergio_eliot@hotmail.com’ ]
> [ ‘data[merges][FNAME]’, ‘Sergio Eliot’ ]
> [ ‘data[merges][LNAME]’, ‘Rodriguez Wong’ ]
> [ ‘data[merges][ADDRESS][addr1]’, ‘El Fuerte No. 2110’ ]
> [ ‘data[merges][ADDRESS][addr2]’, ‘Col. Fovissste’ ]
> [ ‘data[merges][ADDRESS][city]’, ‘MAZATLAN’ ]
> [ ‘data[merges][ADDRESS][state]’, ‘SINALOA’ ]
> [ ‘data[merges][ADDRESS][zip]’, ‘82150’ ]
> [ ‘data[merges][ADDRESS][country]’, ‘MX’ ]
> [ ‘data[merges][PHONE]’, ‘+526691514947’ ]
> [ ‘data[merges][BIRTHDAY]’, ‘10/28’ ]
> [ ‘data[list_id]’, ‘f1f2f46983’ ]
which requires from a developer perspective, to do more string manipulation on the incoming request body.

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:

curl -d 'name=admin&shoesize=12' https://endpoint.m.pipedream.net

event.body should contain:

{
  "name": "admin",
  "shoesize": "12"
}

Let me know if that works for you

Sergio Rodriguez : oh, lovely! that’s like magic)) just made the test with actual Mailchimp payload, and yep, this will do. Thanks