microsoft / microsoft/typespec

[http-server-javascript] Honor `Accept` header

Open
#5,501 0 comments 0 reactions 0 assignees View on GitHub
design:needed emitter:service:js feature triaged:core
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

When the client sends an `Accept` header, the server generator should validate that it is sending a response with the given content-type before it responds (or that it _can_ send such a content type).

Accept-Encoding should also be validated, but that is of lesser value than Accept in this age of UTF-8 everywhere.

`Accept` may also be required when the endpoint may return a data type with multiple serialization formats (e.g. `@header contentType: "application/json" | "application/xml"`, but the JS server generator does not currently support this kind of dynamic serialization/deserialization based on actual content type.

There are interesting cases to consider (as always):

```tsp
op foo(): { @header contentType: "application/json", ...} | { @header contentType: "application/xml" }
```

In the above case, the service implementation can decide to return one type or the other, and they may be nonoverlapping, so the service implementation logic needs to know what, if anything, the client provided in the `Accept` header in order to choose which type to return.

The service implementation can access this information through `HttpContext#request` today, but it might be worth making a first-class property of `HttpContext` if the router/binding implementation has to read and process it anyway.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.