Proxy Server: Amplitude Logger [Error]: Unexpected end of JSON input
MHolmes91 opened this issue · 2 comments
Expected Behavior
A blank success response from an Amplitude proxy endpoint should be handled without any errors being logged to the console as it is on the iOS and Android SDKs when using the "xhr", "beacon", or "fetch" transport options.
Current Behavior
We have implemented an Amplitude proxy endpoint using the Amplitude Go SDK and have set the serverUrl
in amplitude.init()
so that our requests are directed there.
Upon calling track
in @amplitude/analytics-browser
the Network log shows that we are getting a successful response from our proxy server. However, this response is an HTTP 202 response with no body.
This causes an [Error]: Unexpected end of JSON input
error to be logged by the Amplitude API despite the fact that the request is successful.
Normally, it would make sense that this would be a problem with the proxy server implementation, but our iOS and Android clients do not have this same problem since the iOS and Android SDKs do not expect the response from the server to have a body.
Possible Solution
My suggestion would be to make the beacon, xhr, and fetch transport options are able to handle successful HTTP responses that have no body. It seems that if there is no body, there is no reason why an exception should be thrown and an error should be logged since it does not hinder the tracking functionality.
See how it is handled on Android:
Passing the response code and body to the response generator
On a success response, no response body is required or used
The issue is that the JSON.parse()
does not handle falsy values and throws the Unexpected end of JSON input error
:
XHR
Fetch
Steps to Reproduce
- Create a proxy server that returns a blank HTTP body response on success
- Attempt to track an event in Amplitude using the JS SDK.
- Observe the console.error "Amplitude Logger [Error]: Unexpected end of JSON input"
Environment
- JS SDK Version: @amplitude/analytics-browser: 2.6.1
- GO SDK Version: https://github.com/amplitude/analytics-go: 1.0.1
- Installation Method: Yarn
- Browser and Version: Chrome 123
Hi @MHolmes91, I confirm I can reproduce this. This is caused by https://github.com/amplitude/Amplitude-TypeScript/blob/main/packages/analytics-client-common/src/transports/fetch.ts#L20.
const res = new Response('',{status: 200})
res.json()
// SyntaxError: Unexpected end of JSON input
Hi @MHolmes91, @amplitude/analytics-browser@2.8.1 should include the fix.