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

Avoid adding charset to Content-Type header #1438

Open
samrat opened this issue Dec 26, 2024 · 7 comments
Open

Avoid adding charset to Content-Type header #1438

samrat opened this issue Dec 26, 2024 · 7 comments
Labels
package:http type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@samrat
Copy link

samrat commented Dec 26, 2024

Hello! 👋

I'm trying to use the OpenAI Realtime API and need to send Content-Type: application/sdp as one of the headers.

Here's what my Dart code looks like

      final response = await http.post(
        Uri.parse('$baseUrl?model=$model'),
        headers: {
          'Authorization': 'Bearer $ephemeralKey',
          'Content-Type': 'application/sdp',
        },
        body: sdp,
        encoding: null
      );

Even though I'm setting the header to application/sdp the request sent has the header set to application/sdp; charset=utf-8(I tested this with iOS, macOS and Web debug targets)

And looks like the OpenAI endpoint is very strict about the value it accepts for this header:

Response body: {"error":{"message":"Unsupported content type. This API method only accepts 'application/sdp' requests, but you specified the header 'Content-Type: application/sdp; charset=utf-8'. Please try again with a supported content type.","type":"invalid_request_error","param":null,"code":"unsupported_content_type"}}

Is there any way to prevent the http library from modifying headers before the request is made?

@samrat samrat added package:http type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Dec 26, 2024
@seifibrahim32
Copy link

@brianquinlan I will be looking into this issue.

@brianquinlan
Copy link
Collaborator

The documentation for encoding says that setting it will add the charset parameter.

@seifibrahim32
Copy link

@brianquinlan Is it obligatory from http to add charset parameter into the headers?

@brianquinlan
Copy link
Collaborator

"charset" is not required, see https://datatracker.ietf.org/doc/html/rfc2046#section-4.1.2

@samrat I think that your problem is that you are setting the HTTP payload using body and package:http is assuming a default encoding when converting it into bytes. Instead, you can set bodyBytes and use whatever encoding that you want. See https://pub.dev/documentation/http/latest/http/Request/body.html

@seifibrahim32 Changing package:http's charset behavior would be breaking. We may just need better documentation for the top-level functions e.g. https://pub.dev/documentation/http/latest/http/post.html

@seifibrahim32
Copy link

@brianquinlan Yes, charset should be with default utf8 or utf16 atmost.

@samrat Have you tried to use encoding property to maneuver this request?

@chedwin41
Copy link

I am having the same issue here when using an opentelemetry collector. The rules are strict :

The client and the server MUST set “Content-Type: application/json” request and response headers when sending JSON Protobuf encoded payload. (https://opentelemetry.io/docs/specs/otlp/#json-protobuf-encoding)

Here is my code:

http.Response response = await http.post(
              Uri.parse(
                otelUrl,
              ),
              headers: <String, String>{
                "content-type": "application/json",
              },
              body: jsonBody,
            );

As I understand, there is no good solution to avoid "charset=utf-8" to be added in content-type node while using http ? We should directly use request instead ?

@seifibrahim32 If encoding is set to anything else different from utf8 it will encode in another format the body isn't it. What we would need is that the body is still encoded but not specified in the content-type node

@samrat
Copy link
Author

samrat commented Feb 26, 2025

As I understand, there is no good solution to avoid "charset=utf-8" to be added in content-type node while using http ? We should directly use request instead ?

@chedwin41 It seems the way to prevent this behaviour is to set the request body first before setting the headers. Here is what I did: https://samrat.me/til-avoid-modifying-headers-with-dart-http-package/ .

Would love to know if there is a better way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package:http type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

4 participants