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

[dio] Some map response returns null but using Postman can see the field with the correct result #1890

Closed
fakhravari opened this issue Jul 10, 2023 · 4 comments

Comments

@fakhravari
Copy link

Package

dio

Version

5.1.2

Output of flutter doctor -v

No response

Dart Version

No response

Steps to Reproduce

1

Expected Result

I test with package http, it shows responce in the output, but it is null in the output dio.

Screenshot 2023-07-10 150351

Recording.2023-07-10.150840.mp4

Actual Result

UserName:3501578873
Password:12345
IdLanguage:1

https://mobiletest.pmlm.ir/pisc/v3/LoginCustomer

@fakhravari fakhravari added h: need triage This issue needs to be categorized s: bug Something isn't working labels Jul 10, 2023
@fakhravari

This comment was marked as outdated.

@AlexV525 AlexV525 changed the title Null output if displayed in postman [dio] Some map response returns null but using Postman can see the field with the correct result Jul 14, 2023
@AlexV525 AlexV525 added the p: dio Targeting `dio` package label Jul 14, 2023
@AlexV525
Copy link
Member

I can reproduce this using the below code:

import 'package:dio/dio.dart';
import 'package:test/test.dart';

void main() {
  test('request something', () async {
    final response = await Dio().post(
      'https://mobiletest.pmlm.ir/pisc/v3/LoginCustomer',
      data: {'UserName': '3501578873', 'Password': '12345', 'IdLanguage ': 1},
    );
    print(response);
  });
}

@AlexV525 AlexV525 removed the h: need triage This issue needs to be categorized label Jul 14, 2023
@fakhravari
Copy link
Author

fakhravari commented Jul 15, 2023

Screenshot 2023-07-15 093634

import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:dio/dio.dart';
import 'package:http/http.dart' as http;

void main() {
  test('request Dio', () async {
    final response = await Dio().post(
      'https://mobiletest.pmlm.ir/pisc/v3/LoginCustomer',
      data: {'UserName': '3501578873', 'Password': '12345', 'IdLanguage ': 1},
    );
    print(response);
  });

  test('request Http', () async {
    var url = Uri.parse('https://mobiletest.pmlm.ir/pisc/v3/LoginCustomer');
    var response = await http.post(
      url,
      headers: {'Content-Type': 'application/x-www-form-urlencoded'},
      body: jsonEncode(
          {'UserName': '3501578873', 'Password': '12345', 'IdLanguage ': 1}),
    );

    print(response.body);
  });
}
Recording.2023-07-15.094215.mp4

@AlexV525
Copy link
Member

AlexV525 commented Jul 15, 2023

After debugging what's happened to your requests, the dio request is different compared to the http.

For http request:

  • The body will be encoded as UTF-8 bytes in the HttpClientRequest, corresponding length: 60.
  • The body will be sent exactly as an application/x-www-form-urlencoded content as defined.
  • The response returns 1562 bytes, with user: null and Authorization: null.
  • The return AnbarName is پست which corresponds to bytes [217, 190, 216, 179, 216, 170].
  • The response bytes are decoded correctly by the decoder.

For dio and Postman request:

  • The body will be implied as JSON content in the ImplyContentTypeInterceptor because it's a Map, also with application/json as the content-type.
  • The response returns 2588 bytes, with all fields non-null.

To make both requests the same, since user: null and Authorization: null seems abnormal or invalid, I suggest:

  • Do not use jsonEncode for http request, which corrects the usage of the http request.
  • Set Options(contentType: Headers.formUrlEncodedContentType) when using the dio request.

Closing as invalid usages.

@AlexV525 AlexV525 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 15, 2023
@AlexV525 AlexV525 added i: out of support and removed p: dio Targeting `dio` package s: bug Something isn't working labels Jul 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants