-
Notifications
You must be signed in to change notification settings - Fork 120
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
How to calculate response length? #526
Comments
Hi @tib,
Can you describe what you're trying to do first, and we can help you achieve it without using SPIs, which are not guaranteed to be stable? |
I'm simply trying to implement a HEAD response and return the Content-Length. https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD I'm using a JSON response for the GET query ( I was not able to find a corresponding calculate method that I could use for this purpose. I'm looking for a solution something like this (maybe a bit more generic, since this only works with JSON responses): import HTTPTypes
@_spi(Generated) import OpenAPIRuntime
extension APIGateway {
func calculateContentLength<T: Encodable>(_ value: T) throws -> Int64 {
var headerFields = HTTPFields()
let res =
try OpenAPIRuntime.Converter(
configuration: .init()
)
.setResponseBodyAsJSON(
value,
headerFields: &headerFields,
contentType: ""
)
switch res.length {
case .known(let value):
return value
case .unknown:
return 0
}
}
} |
Could you share the OpenAPI definition for the operation you're implementing? From the top of my head, a That said, HEAD request support might be something we need to add first class support for. Would that help you here? |
Hi @tib, are you still interested in this? I think we should consider adding first class support for HEAD requests. |
Yes, this is still an issue for us. 👍 |
@tib Can you share the OpenAPI doc (or at least the snippet) that includes the operation you'd like to implement HEAD for? Just to make sure we focus on the right example. |
openapi: 3.1.0
info:
title: Example API
description: 'Example'
contact:
name: Binary Birds
url: https://binarybirds.com
email: [email protected]
version: 1.0.0
tags:
- name: Example
description: ''
servers:
- url: http://localhost:8080
description: dev
paths:
/example:
head:
tags:
- Example
summary: Example head
description: Example head request
operationId: headOperation
responses:
200:
$ref: '#/components/responses/ExampleResponse'
components:
schemas:
ExampleContentLength:
type: integer
description: Content length
responses:
ExampleResponse:
description: Ok
headers:
Content-Length:
$ref: '#/components/headers/Content-Length'
headers:
Content-Length:
schema:
$ref: '#/components/schemas/ExampleContentLength'
description: Content length header |
Interesting, so what's the reason to include |
@adam-fowler @Joannis @0xTim What's the current handling of HEAD requests in Vapor and Hummingbird? Is this something that should be handled by the transport? |
You can add a flag to the Hummingbird router to auto generate head endpoints (It'll run endpoint but don't return body). |
Great - if Vapor has something similar, then I'd prefer to leave this to the transports to handle. |
Question
Hello,
I'm currently trying to implement a HEAD endpoint, using the Swift OpenAPI generator & runtime. I'm using the following snippet, but I'm aware that it's far from ideal:
Is there a better way to calculate the Content-Length header for HEAD requests? 🤔
Many thanks.
Tib
The text was updated successfully, but these errors were encountered: