Dio options.contentType vs header "Content-Type"

5,258

I've tried this locally using dio: ^3.0.10 and it seems that ContentType.json is an invalid value for contentType.

invalid contentType

Digging through the documentation for dio, Headers.jsonContentType should be used.

valid contentType

Share:
5,258
Magnus
Author by

Magnus

I have delivered value. But at what cost? Bachelor of Science degree in Computer Engineering. ✪ Started out on ATARI ST BASIC in the 1980's, writing mostly "Look door, take key" type games.    ✪ Spent a few years in high school writing various small programs for personal use in Delphi.    ✪ Learned PHP/SQL/HTML/JS/CSS and played around with that for a few years.    ✪ Did mostly Android and Java for a few years.    ✪ Graduated from Sweden Mid University with a BSc in Computer Engineering. At this point, I had learned all there was to know about software development, except where to find that darn "any" key...    ✪ Currently working with Flutter/Dart and Delphi (again).   

Updated on December 13, 2022

Comments

  • Magnus
    Magnus 6 months

    I was trying to make a call to a REST service using the Dio plugin, but kept getting HTTP 400 response code. I thought I was doing everything right by setting the content type and response type options to JSON:

    Response response = await Dio().get(
        'https://api.example.com/v1/products/$productId',
        queryParameters: {},
        options: Options(
            contentType: ContentType.json,
            responseType: ResponseType.json,
            headers: {'Authorization': 'Bearer $MY_API_KEY'}
        ),
    );
    

    However, it turns out that I needed to add a Content-Type header as well:

    headers: {'Authorization': 'Bearer $MY_API_KEY'}, 'Content-Type': 'application/json' };
    

    So now I'm confused - what exactly does the contentType option do? I thought it was analogous to setting the Content-Type header manually?