1

I am trying to send a GET request to a back-end server with some parameters, and I have been getting the error:

DioError [DioErrorType.RESPONSE]: Http status error [500]

The parameters I am sending contains a boolean value and a double too. How can I successfully make this request. I have tried so many answers on Stack Overflow but none worked for me. Below is my code snippet:

Future<Converter> currencyConverter() async {
  String convertURL = baseURL + 'convert/currency/';
  String auth = await localpref.getString('access');

  //  FormData formData = new FormData.fromMap({
  //    "switch": _switch,
  //    "s_currency": senderCurrencyVal,
  //    "r_currency": recieverCurrencyVal,
  //    "amount": amount
  //  });

  Map<String, dynamic> payload = {
    "switch": _switch, //boool
    "s_currency": senderCurrencyVal, // string
    "r_currency": recieverCurrencyVal, // string
    "amount": amount  //double
  };
  try {
    var res = await dio.get(
      convertURL,
      //data: formData,
      queryParameters: payload,
      options: Options(
        followRedirects: false,
        validateStatus: (status) {
          return status < 500;
        },
        headers: {
          "accept": "*/*",
          "Authorization": "Bearer $auth",
          "Content-Type": "application/x-www-form-urlencoded"
        }
      )
    );

    if (res.statusCode >= 200 && res.statusCode <= 250) {
      print("Transfer Succeed");
      var respBody = res.data;

      Converter converter = converterFromJson(res.data);
      print(converter.result);
      return converter;
    } else {
      print(res);
      print("Else not successful");
    }
  } catch (e) {
    print(e);
  }
}

Note: that am not in charge of the backend and I am made to know it was developed with Djangom also I have tried both dio.get and dio.request but I got the same result, formdata doesn't work with GET when using Dio.

This request works well in postman.

I would appreciate your help.

4
  • Did you tried hitting same url in postman with same parameters ? is it working as expected there ? Commented Aug 9, 2020 at 10:08
  • Yest everything works fine in postman Commented Aug 9, 2020 at 10:25
  • Code seems fine. Is this the only request which is not working fine in your project ? Commented Aug 9, 2020 at 11:40
  • yes , my post and patch works fine and even get without parameter also works fine Commented Aug 9, 2020 at 15:12

2 Answers 2

1

Remove the below Header from the request and then try.

"Content-Type": "application/x-www-form-urlencoded"

It tells the backend that you are sending data in Form format.

Sign up to request clarification or add additional context in comments.

Comments

0

Check this out how to fix it Data not showing in list in flutter.

I use http directly for any get.. Also check out https://app.quicktype.io/ to quickly create decoder classes. To use it copy the response from postman here and it will generate a class for you in Dart.

6 Comments

I created a class initially but it was always call on NULL as the api call does not return anything
Also check out the url... There might be a / missing .. when you used concat
i have done that, in fact the URL works fine without parameters, its when i add parameters , that i have that issue
Could you share the full link and sample fields i can try run it
Wait... To send data to receive response you have to use post() that's in http
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.