-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
When calling the Subscription Update Token API using the _baseRequest method, the request sometimes fails with the following error:
Error updating subscription token: FetchError: invalid json response body at
https://subscriptions.solidgate.com/api/v1/subscription/update-token
reason: Unexpected end of JSON input
Observed Behavior
- The API call successfully updates the card token on the subscription.
- However, the
_baseRequestfunction callsres.json()in a.then()chain directly. - Since the
update-tokenAPI returns an empty response body,.json()throws theUnexpected end of JSON inputerror. - As a result, the API call appears to fail, even though the token is updated correctly.
Root Cause
The _baseRequest method assumes all responses contain JSON.
For APIs that return no body, parsing as JSON fails.
Example of problematic code:
fetch(this.baseSolidGateUri + path, {
method: 'POST',
headers: {
'Merchant': this.publicKey,
'Signature': this._generateSignature(data),
'Content-Type': 'application/json'
},
body: data
})
.then(res => resolve(res.json())) // fails if response is empty
.catch(err => reject(err));Steps to Reproduce
- Call the
update-tokenAPI via_baseRequest. - Notice that the card token updates successfully.
- Observe that the promise rejects due to
.json()failing on empty response.
Expected Behavior
_baseRequestshould handle empty response bodies gracefully.- The API call should be considered successful when the card token updates, even if the response is empty.
Suggested Fix / Workaround
Modify _baseRequest to always read the response as text and then try parsing JSON, falling back to the raw text if parsing fails. For example:
fetch(this.baseSolidGateUri + path, {
method: 'POST',
headers: {
'Merchant': this.publicKey,
'Signature': this._generateSignature(data),
'Content-Type': 'application/json'
},
body: data
})
.then(res => res.text()) // always safe
.then(text => {
try {
return resolve(text ? JSON.parse(text) : {});
} catch {
return resolve(text); // fallback if not JSON
}
})
.catch(err => reject(err));Benefits:
- Works with empty responses.
- Preserves JSON responses when they exist.
- Compatible with
.then()-based code without introducingasync/await.
Metadata
Metadata
Assignees
Labels
No labels