如何在Dart中的API POST调用中传递授权标头? [英] How to pass Authorization header in API POST call in dart?
问题描述
我正在使用Flutter应用程序,我想知道我们如何在POST请求中传递授权标头.这是我尝试的代码.我需要帮助来了解如何以正确的方式进行操作.
I'm working with flutter application and I want to know how we pass an authorization header in POST request. Here is the code I tried. I need help to know how to do this in the correct way.
Future<String> addCartItemToDb({String userId}) async {
try {
final res = await http.post(
_baseURL,
headers: {
"Content-Type": "application/json",
"accept": "*/*",
"Authorization": "token"
},
body: jsonEncode({"userId": userID})
);
return _response(res);
}on SocketException {
throw CustomException("No internet Connection");
}on FormatException {
throw CustomException("Bad Response format !");
}
}
此外,我想知道我们如何解码在头中传递的令牌
,以便为已经登录的用户获取 userid
.
Further, I want to know how we decode that token
I passed in the header to get userid
to that user who already logged in.
推荐答案
final jobsListAPIUrl =常量.BASE_URL+'bookings/retail/0/20/?status ='+ constants.BOOKING_CONFIRM_KEY +'& filter = 0';打印(jobsListAPIUrl);var response = await http.get(jobsListAPIUrl,headers:{'Authorization':'Token'+ widget.token});打印(response.body);
final jobsListAPIUrl = constants.BASE_URL+'bookings/retail/0/20/?status='+constants.BOOKING_CONFIRM_KEY+'&filter=0'; print(jobsListAPIUrl); var response = await http.get(jobsListAPIUrl,headers: {'Authorization':'Token '+widget.token}); print(response.body);
这篇关于如何在Dart中的API POST调用中传递授权标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!