Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when doing anonymous call with conditional request using etag #2170

Open
3 of 7 tasks
g-hoang opened this issue Nov 22, 2021 · 3 comments
Open
3 of 7 tasks

Error when doing anonymous call with conditional request using etag #2170

g-hoang opened this issue Nov 22, 2021 · 3 comments
Labels
Type: Bug Something isn't working as documented
Projects

Comments

@g-hoang
Copy link

g-hoang commented Nov 22, 2021

Please avoid duplicates

Reproducible test case

https://runkit.com/g-hoang/octokit-rest-js-2169

Please select the environment(s) that are relevant to your bug report

  • TypeScript
  • Enterprise
  • Browsers
  • Node
  • Deno

Version

+-- @octokit/auth-app@3.6.0
| +-- @octokit/auth-oauth-app@4.3.0
| | +-- @octokit/auth-oauth-device@3.1.2
| +-- @octokit/auth-oauth-user@1.3.0
| | +-- @octokit/oauth-methods@1.2.4
| | | +-- @octokit/oauth-authorization-url@4.3.3
| +-- @octokit/request@5.6.1
| | +-- @octokit/endpoint@6.0.12
| +-- @octokit/request-error@2.1.0
| +-- @octokit/types@6.27.1
| | -- @octokit/openapi-types@10.1.4 +-- @octokit/plugin-retry@3.0.9 +-- @octokit/plugin-throttling@3.5.2 +-- @octokit/rest@18.10.0 | +-- @octokit/core@3.5.1 | | +-- @octokit/auth-token@2.4.5 | | +-- @octokit/graphql@4.8.0 | +-- @octokit/plugin-paginate-rest@2.16.1 | +-- @octokit/plugin-request-log@1.0.4 | -- @octokit/plugin-rest-endpoint-methods@5.10.2

What happened?

Hi,

I'm trying to cache one of my requests using conditional request with etag. All the calls are anonymous. Here is my code in the test case:

const { Octokit } = require('@octokit/rest')
const cache = require('memory-cache')

const octokit = new Octokit({});

const res1 = await octokit.request("GET /gists/{gist_id}", { 
  gist_id: '60e9b5d7ce65ca474c29'
});

cache.put('testCacheKey', res1, 1000 * 60 * 60) // 1 hour

const cachedData = cache.get('testCacheKey')

if (cachedData) {
    try {
        res2 = await octokit.request("GET /gists/{gist_id}", { 
          gist_id: '60e9b5d7ce65ca474c29',
          headers: {
            'If-None-Match': JSON.stringify(cachedData.headers.etag.split('"')[1])
          }
        });
        return res
    }
    catch (error) {
        if (error.status === 304) {
            console.log('Conditional request using etag')
            return cachedData
        } else {
            console.error(error)
        }
    }
}

Output:

RequestError: invalid json response body at https://api.github.com/gists/abczyz123 reason: Unexpected end of JSON input

The first call (res1) seems to work fine, but in the second call, I added headers If-None-Match returned the above output. Most likely I'm doing something wrong here but I cannot find out where is the error.

Thank you in advance and best regards

Would you be interested in contributing a fix?

  • yes
@g-hoang g-hoang added the Type: Bug Something isn't working as documented label Nov 22, 2021
@ghost ghost added this to Bugs in JS Nov 22, 2021
@Chocrates
Copy link

Hey @g-hoang looks like if you replace your header with this it works

headers: {
    "If-None-Match": cachedData.headers.etag.split('"')[1],
}

Looks like it just doesn't like the stringify

@g-hoang
Copy link
Author

g-hoang commented Dec 7, 2021

Hi @Chocrates
Thank you for your response. I will try to test follow your recommendation soon.
Just one thing, actually I implemented the same way with the personal access token (not anonymous) and it's working fine. So I'm not sure if the stringify caused the problem

@Chocrates
Copy link

oh weird @g-hoang, well let me know if it still doesn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something isn't working as documented
Projects
No open projects
JS
  
Bugs
Development

No branches or pull requests

2 participants