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

Ability to set Response Headers #57

Open
Ayrx opened this issue Oct 14, 2020 · 6 comments
Open

Ability to set Response Headers #57

Ayrx opened this issue Oct 14, 2020 · 6 comments

Comments

@Ayrx
Copy link

Ayrx commented Oct 14, 2020

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.

@ahl
Copy link
Collaborator

ahl commented Oct 14, 2020

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.

@Ayrx
Copy link
Author

Ayrx commented Oct 15, 2020

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.

@mx-shift
Copy link

mx-shift commented Oct 15, 2020 via email

@arun-koshy
Copy link

arun-koshy commented Mar 10, 2022

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

@ahl
Copy link
Collaborator

ahl commented Mar 10, 2022

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?

@arun-koshy
Copy link

arun-koshy commented Mar 10, 2022

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

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

No branches or pull requests

4 participants