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

Fetching with Authorization Header #1251

Closed
JCSanPedro opened this issue Aug 26, 2019 · 6 comments · Fixed by #1253
Closed

Fetching with Authorization Header #1251

JCSanPedro opened this issue Aug 26, 2019 · 6 comments · Fixed by #1253

Comments

@JCSanPedro
Copy link

From what I understand, we can fetch files from a private github repository with the following:

dhall <<< 'https://api.github.com/repos/:org/:repo/contents/:path using toMap { Authorization = "token ${env:GITHUB_TOKEN as Text }" }'

but I'm getting a 403 error. I'm not sure what I'm doing wrong here, or what else I can do to debug this further. I have also tried

dhall <<< 'https://api.github.com/repos/:org/:repo/contents/:path using [ { mapKey = "Authorization", mapValue = "token ${env:GITHUB_TOKEN as Text }" } ]'

Additionally, the following curl command does work

curl -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/repos/:org/:repo/contents/:path

The dhall version is 1.25.0.

Any help would be appreciated, thanks.

@Gabriella439
Copy link
Collaborator

@JCSanPedro: Using a fake API token, could you provide the output of this command:

$ dhall <<< 'https://httpbin.org/headers using toMap { Authorization = "token ${env:FAKE_TOKEN as Text}" } as Text'

I haven't been able to reproduce the problem locally, but the output from that might help narrow down the problem.

@JCSanPedro
Copy link
Author

Thanks for the response.

$ dhall <<< 'https://httpbin.org/headers using toMap { Authorization = "token ${env:FAKE_TOKEN as Text}" } as Text'                                                                                  [13:28:10]
''
{
  "headers": {
    "Accept-Encoding": "gzip",
    "Authorization": "token fakeToken",
    "Host": "httpbin.org"
  }
}
''

@Gabriella439
Copy link
Collaborator

@JCSanPedro: Does your GITHUB_TOKEN environment variable contain any whitespace or newlines?

Also, can you paste the error message that you are getting (with any sensitive information redacted)?

My intuition here is that there might be something wrong with the API URL that you are using, but without knowing the path I can't say for certain.

Other than that, the only thing I can think of is to forward your request through a local proxy that logs requests so that you can verify that the header and path look correct.

@JCSanPedro
Copy link
Author

The problem was that the github api requires a valid user-agent header, otherwise it will return a 403.

Not an issue with dhall itself, so I'll close this now, but would it be possible if it returned more than just the status code? Currently it returns

dhall <<< 'https://api.github.com/repos/:org/:repo/contents/:path using
        [ { mapKey   = "Authorization"
          , mapValue = "token ${env:GITHUB_TOKEN as Text}"
          }
        , { mapKey   = "Accept"
          , mapValue = "application/vnd.github.v3.raw"
          }
        ]'

Error: Unexpected HTTP status code:

↳ 403

and the actual error message is

'Request forbidden by administrative rules. Please make sure your request has a User-Agent header (http://developer.github.com/v3/#user-agent-required). Check https://developer.github.com for other possible causes.\r\n'

I initially just assumed I was setting the authorization header incorrectly, until I followed your advice about using a proxy.

Cheers!

@Gabriella439
Copy link
Collaborator

I'll re-open this to improve the error message, but relocate this ticket to the dhall-haskell project since it's an issue specific to the Haskell implementation

@Gabriella439 Gabriella439 transferred this issue from dhall-lang/dhall-lang Aug 27, 2019
Gabriella439 added a commit that referenced this issue Aug 28, 2019
Fixes #1251

This improves HTTP error messages by:

* Expanding the set of recognized status codes
* Adding up to 7 lines of the response body

For example:

```
Error: Remote file missing

URL:

↳ https://httpbin.org/statu

Message:

1│ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
2│ <title>404 Not Found</title>
3│ <h1>Not Found</h1>
4│ <p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p>
```
@Gabriella439
Copy link
Collaborator

The fix is up here: #1253

@mergify mergify bot closed this as completed in #1253 Aug 29, 2019
mergify bot pushed a commit that referenced this issue Aug 29, 2019
* Improve HTTP errors

Fixes #1251

This improves HTTP error messages by:

* Expanding the set of recognized status codes
* Adding up to 7 lines of the response body

For example:

```
Error: Remote file missing

URL:

↳ https://httpbin.org/statu

Message:

1│ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
2│ <title>404 Not Found</title>
3│ <h1>Not Found</h1>
4│ <p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p>
```

* Always include HTTP status code in output

... as suggested by @sjakobi

* Use "not found" wording for 404 message

This is closer to the official wording for the error message

* Compress error message formatting

There's no need to put the URL and status code on separate lines since
they are compact
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants