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

add example that shows accessing request headers #374

Merged
merged 3 commits into from
Jun 13, 2022
Merged

Conversation

davepacheco
Copy link
Collaborator

Someone asked me about how to access request headers from a dropshot handler, so I threw together an example that shows this.

@davepacheco davepacheco requested a review from ahl June 13, 2022 22:04
@davepacheco
Copy link
Collaborator Author

I ran this example like this:

$ cargo run --example=request-headers
...
Jun 13 21:59:14.178 INFO listening, local_addr: 127.0.0.1:56495

Here are some example requests using it. First, without specifying the demo header:

$ curl -i 127.0.0.1:56495/header-example-generic
HTTP/1.1 200 OK
content-type: application/json
x-request-id: 7b5f1329-c01a-44a8-8e51-4766621700ff
content-length: 24
date: Mon, 13 Jun 2022 21:59:27 GMT

"value for header: None"

Now, specifying a value:

$ curl -H 'demo-header: demo-value' -i 127.0.0.1:56495/header-example-generic
HTTP/1.1 200 OK
content-type: application/json
x-request-id: fcbd1a7f-0978-492b-b9c5-9fe8b4c267fb
content-length: 40
date: Mon, 13 Jun 2022 21:59:49 GMT

"value for header: Some(\"demo-value\")"

If you specify two values, you only get one of them:

$ curl -H 'demo-header: demo-value-1' -H 'demo-header: demo-value-2' -i 127.0.0.1:56495/header-example-generic
HTTP/1.1 200 OK
content-type: application/json
x-request-id: ba14f906-d9ff-4b7d-8749-17836823971d
content-length: 42
date: Mon, 13 Jun 2022 22:07:22 GMT

"value for header: Some(\"demo-value-1\")"

I confirmed with curl -v that curl is sending both, but the code in the example uses http::HeaderMap::get, which only returns the first value. There's a comment in the example about this and pointing people at the docs for getting other values out.

Copy link
Collaborator

@ahl ahl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's get that money

@davepacheco davepacheco enabled auto-merge (squash) June 13, 2022 22:11
@davepacheco davepacheco merged commit 27c2fa2 into main Jun 13, 2022
@davepacheco davepacheco deleted the example-headers branch June 13, 2022 22:19
@StaringSkyward
Copy link

This is exactly what I needed to figure out JWT auth with dropshot. Thanks @davepacheco!

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 this pull request may close these issues.

3 participants