0

How to send GET request with body in dart?

I tried all recipes from SO (this and that and more) but with no success.

final queryParameters = {
  "id": "6767676767676",
  "device": "tatatata",
  "user": {
    "login": "login",
    "password": "pwd",
  }
};

This is complex JSON and i got an error when send request: final uri = Uri.http('44.44.444.444:8080', 'get-answer', queryParameters);

Exception: type '_InternalLinkedHashMap<String, Object>' is not a subtype of type 'Map<String, String>'

Valery Kulikov
  • 319
  • 1
  • 12

4 Answers4

0

I think your queryParameters should not contain objects.

final queryParameters = {
    "id": "6767676767676",
    "device": "tatatata",
    "login" : "login",
    "password" : "password",
};
0

have you tried :

var response = await get(url);
var fetchedData = json.decode(response.body);

fetchedData will be in a json map format

Rechem MEG
  • 311
  • 1
  • 10
0

The answer its actually what Flutter has suggested in this part of exception message :

'_InternalLinkedHashMap<String, Object>' is not a subtype of type 'Map<String, String>'

What does it mean ?

It means that, one of your variable is '_InternalLinkedHashMap<String, Object>' and flutter wants you to change it to 'Map<String, String>' type

You should make one of your key in queryParameters to this 'Map<String, String>' type

How ?

1. Import Dart Convert

by importing dart:convert, we will have jsonEncode method


    import 'dart:convert'; // put this at top of your file

2. Convert Nested Value, to String


    final nestedValue = {
        "login": "login",
        "password": "pwd",
      };

    String stringValue = jsonEncode(nestedValue); 

3. Put it back to your main Parameters


    final queryParameters = {
      "id": "6767676767676",
      "device": "tatatata",
      "user": stringValue
    };

    final uri = Uri.http('44.44.444.444:8080', 'get-answer', queryParameters);

Final Code

This will be your final code looks like below :


    import 'dart:convert'; // put this at top of your file


    final nestedValue = {
        "login": "login",
        "password": "pwd",
      };

    String stringValue = jsonEncode(nestedValue); // add this

    final queryParameters = {
      "id": "6767676767676",
      "device": "tatatata",
      "user": stringValue
    };

    final uri = Uri.http('44.44.444.444:8080', 'get-answer', queryParameters);
ejabu
  • 2,998
  • 23
  • 31
  • nope((( Now parameters successed, but i got error: `Response body: {..."status":400,"error":"Bad Request","message":"Required request body is missing: ` As i can realized `queryParameters` is not really `body`. – Valery Kulikov Jun 20 '20 at 18:00
  • 1
    Sure, its different problem. I think you use JSON endpoint right ?, its different than you use now. You better use package dio. But if you find that your previous problem is now solved, Exception: type '_InternalLinkedHashMap' is not a subtype of type 'Map', you should accept this as answer then it will closed automatically. – ejabu Jun 21 '20 at 03:45
  • Thanx, you're right, your advice really helped with Map<...>. I tried dio too, it did not help to solve my "GET with body" issue. Will dig sources )) – Valery Kulikov Jun 21 '20 at 03:55
  • oh I see, previously I had this issue. I had to put Content-type Application/json on the header. and put {} on the body – ejabu Jun 21 '20 at 04:03
0

I advice you to use the http package it is perfect for performing http requests. before sending data make sure you encode you map into json format using the jsonEncode function and to obtain the data from a GET request decode the body of the request from json format using the jsonDecode function.

  • These functions are imported from the dart:convert package
//import the packages
import 'package:http/http.dart' as http;
import 'dart:convert';

//other code here

final queryParameters = jsonEncode( {
"id": "6767676767676",
"device": "tatatata",
"login" : "login",
"password" : "password",
});

//we send the data to the server with the post method
await http.post("44.44.444.444:8080",queryParameters);

similarly for retrieving data:

final res = http.get("getUrl");

//if the request is successful we get the data
if(res.statusCode == 200)
    final data = jsonDecode(res.body);
Mich25educ
  • 295
  • 1
  • 15