dart 为什么Put方法在flutter中没有发送任何响应

wj8zmpe1  于 12个月前  发布在  Flutter
关注(0)|答案(1)|浏览(94)

我有一个API,必须使用put方法,但不工作。我尝试了太多的方法,但没有工作。好吧,我不知道为什么不工作的Put方法或可能是我的错。然而,在Postman的Put方法是正确的,我不能在Flutter中修复它。
按照我的send-put方法代码

我的put请求参数

{
    "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部分,它不发送put方法。

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}');


日志
x1c 0d1x的数据
我试过这个方法,但我不能。

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",
    },
  ),
);

yeotifhr

yeotifhr1#

你应该将你的响应从json解码到Map,你可以使用jsonDecode来解码你的响应,看下面的例子,

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
}

字符串
这是如何使用jsonDecode的例子。
当我们用你的代码尝试它时,我们可以得到下面的更新,

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
);


所以你的请求发送部分是好的,因为我知道,

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",
    },
  ),
);

相关问题