tinybirdco / web-analytics-starter-kit

Tinybird Web Analytics Starter Kit
https://tinybird.co/starter-kits/web-analytics
MIT License
350 stars 36 forks source link

[Bug] flock.js `_sendEvent` should send `Content-Type` `application/json` #33

Closed grempe closed 2 years ago

grempe commented 2 years ago

Module

Tracker

Describe the Bug

In the javascript tracker code the post request is sending stringified JSON content with the content type header set to text/plain.

https://github.com/tinybirdco/web-analytics-starter-kit/blob/3bd8da7c7bebb3e9f4773dc63e659e1dfbf8cebd/middleware/src/index.js#L94

This prevents the server from doing proper content negotiation. So for example the Next.js router will not automatically parse this incoming content to an Object.

https://nextjs.org/docs/api-routes/request-helpers

req.body - An object containing the body parsed by content-type, or null if no body was sent

Instead the Content-Type request header should be set to application/json

Expected Behavior

The POST call from the tracker should set the correct content-type.

To Reproduce

Use the flock.js tracker to point towards a server that parses content correctly based on request content-type header.

Details

No response

alejandromav commented 2 years ago

Fixed in version 1.2.2, just published.

grempe commented 2 years ago

Cool. Works. I've updated my code to remove the JSON.parse() and the bug ref.

https://github.com/grempe/nextjs-tinybird-example/blob/main/pages/api/tracking.ts#L87