gridaco / cors.sh

Sick of CORS Errors?
https://cors.sh
MIT License
185 stars 10 forks source link

Problems with `open.tiktokapis.com` (And others) #38

Open softmarshmallow opened 1 year ago

softmarshmallow commented 1 year ago

Reported Problem: While interacting with tiktokapis, the proxy server will response with 502, causing cors error on browser console.

The error is caused by url encoding, which returns 401 (sort of ok) https://open.tiktokapis.com/v2/user/info/?fields=open_id,union_id,avatar_url,display_name,bio_description,profile_deep_link but our proxy.cors.sh automatically encodes the url, which will result below https://open.tiktokapis.com/v2/user/info?fields=open_id%2Cunion_id%2Cavatar_url%2Cdisplay_name%2Cbio_description%2Cprofile_deep_link If you try this in the browser, you’ll notice that the first will print out the valid json and the second will throw nginx 404 error. We are now finding a way to prevent this. (In most cases the target server should except the encoded params as a valid query - Unfortunately, tiktok doesn’t)


What I’ve found is that tiktok has very poor api server, that we should always have a trailing slash before the query params.

https://open.tiktokapis.com/v2/user/info?fields=open_id
https://open.tiktokapis.com/v2/user/info/?fields=open_id
                                         ^ slash required

Solution:

We are adding new header x-strict-request-url Which users can set the same request url, but preventing it from getting altered (url-encoded, trailing slash being removed)

You can use this the same way you did, but with extra header

// example with tiktok api
fetch("https://proxy.cors.sh/https://open.tiktokapis.com/v2/user/info/?fields=open_id", {
     headers: {
         "x-cors-api-key": "<your-cors.sh-api-key>",
         "Authorization": "Bearer <your-tiktok-token>",
         // NEW (enter the same url)
         "x-strict-request-url": "https://open.tiktokapis.com/v2/user/info/?fields=open_id"
    }
})