Reputation: 309
Got a small app that makes API requests for verification to an external endpoint, which has worked up until now.
The cURL request is as follows:
curl https://api.gumroad.com/v2/licenses/verify \
-d "product_permalink=PRODUCT_PERMALINK" \
-d "license_key=INSERT_LICENSE_KEY" \
-X POST
And returns a success
response.
Turning that into a Fetch
request returns Error 422
. I'm using https://cors-anywhere.herokuapp.com/
, as it otherwise gets blocked by CORS policy. The request is as follows:
fetch("https://cors-anywhere.herokuapp.com/https://api.gumroad.com/v2/licenses/verify", {
body: "product_permalink=SCpVP&license_key=INSERT_LICENSE_KEY",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
method: "POST"
})
This used to work, but it suddenly does not. The response error is SyntaxError: Unexpected token < in JSON at position 0
, which I don't quite understand.
How would I fix my fetch request to work, much like the cURL request?
Upvotes: 0
Views: 847
Reputation: 109
You might have been getting rate limited. As cors-anywhere's readme says,
This server is only provided so that you can easily and quickly try out CORS Anywhere. To ensure that the service stays available to everyone, the number of requests per period is limited, except for requests from some explicitly whitelisted origins.
If you expect lots of traffic, please host your own instance of CORS Anywhere, and make sure that the CORS Anywhere server only whitelists your site to prevent others from using your instance of CORS Anywhere as an open proxy.
Using your code still wouldn't work for me though— I kept getting an error message that there weren't any products for the permalink I'd supplied.
However, this code from Ed works perfectly fine, if you combine it with cors-anywhere:
fetch("http://cors-anywhere.herokuapp.com/https://api.gumroad.com/v2/licenses/verify", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
product_permalink: "<YOUR-PERMALINK>",
license_key: "<YOUR-LICENSE—KEY>",
increment_uses_count: false,
}),
})
Upvotes: 1