Skip to content

Ability to set Response Headers #57

Open

Description

@Ayrx

Hi,

I would like the ability to set specific HTTP response headers on specific endpoints. E.g. it would be nice to have the ability to set a Content-Disposition header at specific endpoints to trigger a file download when accessed via a browser.

I do not see this currently exposed in the API anywhere but maybe I missed something.

Activity

  1. ahl commented on Oct 14, 2020

    @ahl
    Collaborator

    Hey @Ayrx -- you're right: not something we support today. We'll be happy to take a look, but let us know if there's some particular way you think this would work well from consumer code.

  2. Ayrx commented on Oct 15, 2020

    @Ayrx
    Author

    I haven't actually looked at the implementation of the macro and Dropshot in general so I'm not sure if it is actually feasible but I'd be happy with a string-interface similar to the following:

    #[endpoint {
        method = GET,
        path = "/foo",
        headers = "Content-Disposition: xxx; X-Random-Header: xxx",
    }]
    async foo(

    In addition, it would be nice to have a method on HttpServer to add a header that applies to all endpoints since I understand that dropshot is intended to be used as a standalone HTTP server and not used behind something like nginx as a proxy:

    server.add_header("X-Random-Header", "XXX");

    As far as resolving conflicting headers, I think having headers defined at a API level overriding globally defined header values should work fine.

  3. mx-shift commented on Oct 15, 2020

    @mx-shift
  4. arun-koshy commented on Mar 10, 2022

    @arun-koshy

    Is there any update on this? I am trying to find out the right way to do something similar for CORS headers using dropshot

    I currently seem to be able to get responses via Fetch API if I use the following handler response

    Response::builder() .status(StatusCode::OK) .header(http::header::CONTENT_TYPE, CONTENT_TYPE_JSON) .header(http::header::ACCESS_CONTROL_ALLOW_ORIGIN, "*") .body(body)?;

    However I still see an error in the server logs with the OPTIONS preflight request

    INFO request completed, error_message_external: Method Not Allowed, error_message_internal: Method Not Allowed, response_code: 405, uri: /addresses, method: OPTIONS, req_id: f0323b5a-6880-42c6-9bcb-add513d80751, remote_addr: 127.0.0.1:58042, local_addr: 127.0.0.1:5000

  5. ahl commented on Mar 10, 2022

    @ahl
    Collaborator

    I believe that #276 may address the needs described initially in this issue both via headers expressed in the OpenAPI description and ad hoc headers.

    @arun-koshy I think we may need more context to understand the pathology you're describing. Does your code work if you omit the .header() calls?

  6. arun-koshy commented on Mar 10, 2022

    @arun-koshy

    If I omit .header(http::header::ACCESS_CONTROL_ALLOW_ORIGIN, "*") the response will not be accepted by Fetch API. This is because of CORS. According to this article to get around that you need to include the header above.

    If I do a simple GET request with the header I believe everything is fine. But if it is not a simple request, I think the '405: Method Not Allowed' error I am seeing with the header included may be a result of CORS support not being available in Dropshot? Specifically the OPTIONS method not being supported?

    Does dropshot have something similar to how rocket solves this with the rocket_cors crate

  7. kyrias commented on Jun 22, 2026

    @kyrias

    I've been looking at potentially starting to use Dropshot for an API of mine and while looking at the API versioning support I was a bit confused as to how to correctly implement header-based API versioning, and it seems to me like it's not currently possible (or at least feasible) to do this entirely correctly?

    There is ClientSpecifiesVersionInHeader for telling Dropshot which header to fetch the version from, but it doesn't have any way of setting the Vary header accordingly, so if one were to just use that and then have one's server end up behind a caching proxy then one would be in for a bad time when the proxy returns the cached responses for mismatched API versions.

    It seems very unfortunate to have make each endpoint set the Vary header to include the header that ClientSpecifiesVersionInHeader looks for.

  8. davepacheco commented on Jun 23, 2026

    @davepacheco
    Collaborator

    @kyrias That could be right. We don't use caching and so haven't run into this. What would you suggest? Should (could?) use of ClientSpecifiesVersionInHeader implicitly cause the Vary header to be set to include this header?

  9. kyrias commented on Jun 23, 2026

    @kyrias

    What would you suggest? Should (could?) use of ClientSpecifiesVersionInHeader implicitly cause the Vary header to be set to include this header?

    I would say that it definitely should, yes.

  10. davepacheco commented on Jun 26, 2026

    @davepacheco
    Collaborator

    @kyrias Makes sense. Would you mind filing a new issue for that?

  11. kyrias commented on Jun 26, 2026

    @kyrias

    Done! #1628.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions