简体   繁体   中英

DioErrorType.RESPONSE invalid media type

My flutter project runs well on android but when trying to get it to web I get some troubles. I am using Graphql Faker as fake backend and it works well for android but for flutter web it always throw below error

╔╣ Request ║ POST 
║  http://localhost:9002/graphql
╚══════════════════════════════════════════════════════════════════════════════════════════

╔╣ DioError ║ Status: 500 Internal Server Error
║  http://localhost:9002/graphql
╚══════════════════════════════════════════════════════════════════════════════════════════
╔ DioErrorType.RESPONSE
║    {
║         errors: [{message: invalid media type}]
║    }
╚══════════════════════════════════════════════════════════════════════════════════════════

Below is how client looks

GraphQLClient graphQLClient()  {
  final dio = Dio(
    BaseOptions(
      connectTimeout: 3 * 1000,
      contentType: 'application/json',
    ),
  );

  final host = () {
    try {
      if (Platform.isAndroid) return MY_IP;
    } catch (_) {}
    return 'localhost';
  }();

  final graphqlEndpoint = 'http://$host:9002/graphql';
  final WebSocketLink _wsLink = WebSocketLink('ws://$host:4000/graphql');

  final Link _dioLink = DioLink(
    graphqlEndpoint,
    client: dio,
  );

  // split request based on type
  final _link = Link.split(
    (request) => request.isSubscription,
    _wsLink,
    _dioLink,
  );

  return GraphQLClient(
    cache: GraphQLCache(),
    link: _link,
  );
}

Any comment/answer to fix this will be appreciated

This looks like a CORS error, the graphql-faker documentation has a section about cors:

--cors-origin CORS: Specify the custom origin for the Access-Control-Allow-Origin header, by default it is the same as Origin header from the request

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM