Skip to content

WP-6213 Static methods on Http class do not allow content-type header to be explicitly set #302

@evanweible-wf

Description

@evanweible-wf

Because the static request methods on the Http class are implemented by creating a Request instance and setting the uri and headers fields, there is an issue where the content-type header cannot be defined because the Request class is assumed to be of content-type text/plain. In other words, the following does not work as one would expect:

final uri = ...;
Http.post(uri, headers: {'content-type': 'application/json'});
// Should send a request with content-type: application/json
// but actually sends a request with content-type: text/plain

The workaround is to construct the appropriate request class instance and modify the content-type via the contentType field:

// Some content-types like application/json are handled by classes
// provided by w_transport:
final jsonRequest = new JsonRequest();
jsonRequest.post(); // will be sent with content-type: application/json

// If there isn't a class that supports your desired content-type,
// you can construct a regular `Request` and set it manually:
final xmlRequest = new Request()
  ..contentType = new MediaType('application', 'xml');
xmlRequest.post(); // will be sent with content-type: application/xml

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions