plausible / analytics

Simple, open source, lightweight (< 1 KB) and privacy-friendly web analytics alternative to Google Analytics.
https://plausible.io
GNU Affero General Public License v3.0
20.72k stars 1.1k forks source link

The curl sample in https://plausible.io/docs/events-api returns 400 status code. #1805

Closed andyj2022 closed 2 years ago

andyj2022 commented 2 years ago

Past Issues Searched

Issue is a Bug Report

Using official Plausible Cloud hosting or self-hosting?

Plausible Cloud from plausible.io

Describe the bug

The curl sample in https://plausible.io/docs/events-api returns 400 status code.

curl -i -X POST https://plausible.io/api/event -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 OPR/71.0.3770.284' -H 'X-Forwarded-For: 127.0.0.1' -H 'Content-Type: application/json' --data '{"name":"pageview","url":"https://plausible.io","domain":"plausible.io","screen_width":1666}'

Returns 400 error "Bad Request".

Making any POST /api/event test from any browser (Chrome, Firefox), it always return CORS error, and it seems to be produced because "access-control-allow-headers" from "plausible.io" does not allow sending "X-Forwarded-For":

access-control-allow-headers: Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since,X-CSRF-Token

Expected behavior

Return ok status code.

Screenshots

No response

Environment

- OS: Ubuntu 20.04
- Browser: Firefox, Chrome
- Browser Version: Latest.
ukutaht commented 2 years ago

Not for me. Any idea how I could replicate your problem?

➜  analytics git:(master) curl -i -X POST https://plausible.io/api/event -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 OPR/71.0.3770.284' -H 'X-Forwarded-For: 127.0.0.1' -H 'Content-Type: application/json' --data '{"name":"pageview","url":"https://plausible.io","domain":"plausible.io","screen_width":1666}'
HTTP/2 202 
date: Tue, 29 Mar 2022 10:08:31 GMT
content-type: text/plain; charset=utf-8
content-length: 2
server: BunnyCDN-SE1-725
cdn-pullzone: 682664
cdn-uid: 153cb5b1-399a-48ef-b5bf-098c03770254
cdn-requestcountrycode: EE
access-control-allow-credentials: true
access-control-allow-origin: *
cache-control: must-revalidate, max-age=0, private
permissions-policy: interest-cohort=()
x-request-id: FuDSIw9tTU50Gq2xJPkD
cdn-proxyver: 1.02
cdn-requestpullsuccess: True
cdn-requestpullcode: 202
cdn-cachedat: 03/29/2022 10:08:31
cdn-edgestorageid: 725
cdn-status: 202
cdn-requestid: 2d91c3c7d381ce25ac4bc59a241e870b

ok% 
andyj2022 commented 2 years ago

Sorry. I was using the wrong version of 'curl' on an Ubuntu virtual machine. To top it off, in the browser tests I was manually setting 'User-Agent' and 'X-Forwarded-For', which is prohibited.

ukutaht commented 2 years ago

No worries :)

MichaelOrpheo commented 1 year ago

I am having the exact same issue, but I am using the Flutter plugin plausible_analytics 0.3.0.

The plugin is setting the X-Forwarded-For header to 127.0.0.1, which seems to be causing the error. The issue is happening with Chrome and Firefox, but not Safari.

It seems that this CORS error is only happening when the browser is sending an OPTIONS request. I tried the curl command mentioned above and it worked.

I think that Safari is not sending OPTIONS requests, which is also the case for the curl command, and I think that's why it's working.

Am I missing something, or is there an error on the plausible.io cloud side?

ukutaht commented 1 year ago

Isn't flutter a framework for mobile apps? I haven't heard of flutter apps running in browsers.

What Content-type does the flutter plugin send?

MichaelOrpheo commented 1 year ago

Flutter is also used to build web apps. It's what I am using here.

The plugin is setting these headers:

If I remove the X-Forwarded-For header, it's working fine but I am not sure it's the right solution.

ukutaht commented 1 year ago

Content-Type to application/json; charset=utf-8

Try changing the content-type to text/plain. That's what we do in the official client to avoid OPTIONS request.