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

Unable to override Content-Type header #630

Open
ashitaprasad opened this issue Mar 2, 2025 · 9 comments
Open

Unable to override Content-Type header #630

ashitaprasad opened this issue Mar 2, 2025 · 9 comments
Labels

Comments

@ashitaprasad
Copy link
Member

Describe the bug/problem

Create a new POST request as shown below with JSON body

Image

If you do not specify any content-type in header, the client automatically sets it to application/json; charset=utf-8.

In the headers, you can specify the Content-Type as application/json to override the default and send request. When you inspect headers in response pane you can observe that the Request header that was sent for Content-Type was application/json; charset=utf-8 instead of application/json

Image

`charset=utf-8' is automatically added into 'Content-Type', and we are unable to remove it as for some APIs, this header string can cause rejection.

Similarly, if you set text/csv as Content-Type in Request and send the response. Again charset=utf-8' is being automatically added by http` library.

Image

The objective is that user should have full control over the Content-Type header that is being sent to the server.

Relevant issues:

@AffanShaikhsurab
Copy link
Contributor

i was able ot reproduce the issue : we should allow the user to have a total control over the content type . I will look into it and see how we can solve this

@AffanShaikhsurab
Copy link
Contributor

@ashitaprasad should we also allow the user to choose the encoding ?

@Jaishree2310
Copy link

@AffanShaikhsurab Are you working on this issue ?

I wanted to work on this if needed can both work together?

@AffanShaikhsurab
Copy link
Contributor

Hi @Jaishree2310,

I've already created a pull request (#631) to address this issue. However, if you have any suggestions or improvements, feel free to review the PR and share your thoughts. I'd be happy to collaborate or make any necessary refinements.

@ashitaprasad
Copy link
Member Author

ashitaprasad commented Mar 2, 2025

@ashitaprasad should we also allow the user to choose the encoding ?

@AffanShaikhsurab The default encoding should be utf-8, if the user specifies a particular encoding using header field Content-Type like application/json; charset=utf-16 or application/json; charset=iso-8859-1 or application/json; charset=us-ascii. Then the body must be encoded using the specified encoding.
https://api.flutter.dev/flutter/dart-convert/Encoding/getByName.html

@AffanShaikhsurab
Copy link
Contributor

AffanShaikhsurab commented Mar 3, 2025

@ashitaprasad Thanks for clarifying , the current implementation in the pr satisfies the above functionality

Image

@ashitaprasad
Copy link
Member Author

@ashitaprasad Thanks for clarifying , the current implementation in the pr satisfies the above functionality

Image

No @AffanShaikhsurab it doesn't. The Request header for Content-Type being sent is application/json; charset=utf-16 but the actual body is being encoded in utf-8

@AffanShaikhsurab
Copy link
Contributor

@ashitaprasad Apologies for the oversight! I've updated it to support multiple encoding types. If you have any further suggestions, I'd be happy to implement them!

@Aman071106
Copy link

Aman071106 commented Mar 9, 2025

@ashitaprasad

Issue Summary: Charset=utf-8 in HTTP Headers

Objective

Investigate how the http client (in Dart) handles the Content-Type header and whether charset=utf-8 is automatically appended.

Findings:

  • Default Behavior: The http package in Dart automatically appends charset=utf-8 to the Content-Type: application/json header when sending a request.
  • Expected Behavior: When sending application/json, it is expected to append charset=utf-8 unless manually overridden.
  • Request Testing: Removing charset=utf-8 from the Content-Type header does not prevent it from being appended.

Relevant Code Snippets:

Code to check auto-appended charset=utf-8:

import 'package:http/http.dart' as http;

void main() async {
  var uri = Uri.parse('https://api.apidash.dev/case/lower');
  String body = '''{
    "text": "I LOVE Flutter."
  }''';
  var headers = {
    'Content-Length': '0',
    'Content-Type': 'application/json',
  };

  final response = await http.post(uri, headers: headers, body: body);
  print(response.headers); // Check the headers here
}

Dart HTTP Documentation:
According to the Dart HTTP library documentation, the content-type header is typically auto-calculated, and for JSON, it defaults to UTF-8 encoding unless explicitly stated otherwise. Dart HTTP package documentation.

Overriding the Default Encoding:
You can override the default encoding like so:

var headers = {
  'Content-Type': 'application/json; charset=ISO-8859-1',
};

More Findings:

Some UI issues raised: #644
Even the Content-Length gets auto-calculated if it is incorrect.

Interest in Google Summer of Code (GSoC)

I am highly interested in participating in Google Summer of Code (GSoC) and am eager to contribute to open-source projects. My skills include:

Programming Languages: C++, Python
Mobile Development: Android (Kotlin), Flutter
Version Control: Git, GitHub
AI/ML: Generative AI(LLMs)
Competitive Programming: Experienced in problem-solving and algorithm design
Blockchain Development: Solidity, Hardhat, Aptos

I am particularly interested in projects related to mobile development,, generative AI. I believe my passion for learning and technical skills make me a strong candidate for contributing to innovative open-source projects during GSoC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants