Search code examples
flutterdartpostman

Why doesn't the Put method did not send any response in flutter


I have an API that must using put method but doesn't work. I tried too many ways but none worked. well, I don't know why doesn't work the Put method or maybe it is my fault. however, the Put method in Postman is correct, I can't fix it in Flutter.


Following my code for the send-put method

My parameters used on the put request

{
    "phone_number": "155556485515", // required
    "full_name": "AMIN AZIZZADEH", // required
    "admin_role": "1", // required| 1 => admin | 2 => operator
    "password": "#edg$#sdghd", // required
    "password_confirmation": "#edg$#sdghd", // required
    "email": "[email protected]", // nullable
    "is_active": "0", // 0 => active | 1 => deactive
    "national_id": "125852", // nullable | shomare shenasname
    "address": "US" // nullable
}

Send method :

It is just the part of the send that is in my code that doesn't send put method.

    Map<String, String> authorization = {};
    Map<String, dynamic> bodyMap = {};

    var uri =
        Uri.parse(config.url);
    var response = await http.put(
      uri,
      headers: {
        HttpHeaders.authorizationHeader:'Bearer ${config.token}',
        HttpHeaders.acceptHeader: 'application/json',
      },
      body: bodyMap,
    );
    print('response boz -> ${response.body}');

Log

enter image description here

I tried with the method, but I could not.

var response = await put(
  config.url,
  headers: authorization,
  jsonEncode(
    <String, String>{
"phone_number": "155556485515", 
"full_name": "AMIN AZIZZADEH", 
"admin_role": "1", 
"password": "#edg$#sdghd",
"password_confirmation": "#edg$#sdghd",
"email": "[email protected]",
"is_active": "0",
"national_id": "125852",
"address": "US",
    },
  ),
);

Solution

  • You should decode your response from json to Map, you can use jsonDecode for decode your response, look at bellow example,

    import 'dart:convert';
    
    void main() {
      String jsonString = '{"name": "Amin", "age": 25}';
      Map<String, dynamic> decodedMap = jsonDecode(jsonString);
    
      print(decodedMap['name']); // Output: Amin
      print(decodedMap['age']);  // Output: 25
    }
    

    this is example for how to use jsonDecode.

    When we try it with your code we can get bellow updates,

    var response = await http.put(
      uri,
      headers: {
        HttpHeaders.authorizationHeader: 'Bearer ${config.token}',
        HttpHeaders.acceptHeader: 'application/json',
        HttpHeaders.contentTypeHeader: 'application/json',
      },
      body: jsonEncode(bodyMap), // Encode bodyMap to JSON format
    );
    

    so your request sending part is ok as my knowladge,

    var response = await put(
      config.url,
      headers: authorization,
      jsonEncode(
        <String, String>{
    "phone_number": "155556485515", 
    "full_name": "AMIN AZIZZADEH", 
    "admin_role": "1", 
    "password": "#edg$#sdghd",
    "password_confirmation": "#edg$#sdghd",
    "email": "[email protected]",
    "is_active": "0",
    "national_id": "125852",
    "address": "US",
        },
      ),
    );