Too many Request (429) error when using Flutter Http library to send a Post request - flutter

The code below returns a 429 status code. What's the issue
const Map<String, String> _headers = {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer 9|YNYTWEW89LI1NOM1lf7TxcZZSP',
};
Uri url = Uri.parse('http://7981.00/postdata/');
Map<String, dynamic> data = {};
data['first'] = 'data1';
data['dataList'] = List<Map<String, dynamic>> listData
String _body = jsonEncode(orderTable);
http.Response response = await http.post(url, headers: _headers, body: _body);
print(response.statuscode);

Related

Fail to get http response with form-data

I am not able to get response from http POST in Flutter but URL and data are verified in Postman.
var map = new Map<String, String>();
final url = Uri.parse(globals.ServerDomain + '/login');
Map<String, String> requestBody = <String, String>{
'username': '80889099',
'password': '123456789abcde'
};
var request = http.MultipartRequest('POST', url)
..fields.addAll(requestBody);
var response = await request.send();
final respStr = await response.stream.bytesToString();
print(respStr);
no result returned.
Flutter has this great package called Dio to handle all sort of http requests. It's very easy to do what you want with it, you are using form data so this is what you should use. For more details check this https://pub.dev/packages/dio#sending-formdata
Example code:
final formData = FormData.fromMap(
{'username': '80889099',
'password': "123456789abcde",
});
final response = await dio.post('${globals.ServerDomain}/login', data: formData);
Looks like Its big project changing the package is not a good option try adding request header
'Content-Type': 'multipart/form-data'
example code :
Map<String, String> headers= <String,String>{
'Authorization':'Basic ${base64Encode(utf8.encode('user:password'))}',//your any other header
'Content-Type': 'multipart/form-data'
};
var map = new Map<String, String>();
final url = Uri.parse(globals.ServerDomain + '/login');
Map<String, String> requestBody = <String, String>{
'username': '80889099',
'password': '123456789abcde'
};
var request = http.MultipartRequest('POST', URL)
..headers.addAll(headers)
..fields.addAll(requestBody);
var response = await request.send();
final respStr = await response.stream.bytesToString();
print(respStr);
Additionally check if internet permission is given (may not be the cause just info)

how to send token while post request

have a great day, i have a silly problem, don't mind as i am noob. ok my problem is, i have a token which i received after i login, now i have to post a data but for that i have to include this token in my header, but i don't know how to....
here is my token, which i received after login as response
{
"token": "8d18265645a87d608868a127f373558ac2e131a6"
}
Here, i have to implement in flutter
apiData.ApiData app = apiData.ApiData();
final String apiURl = app.api;
SharedPreferences pref = await SharedPreferences.getInstance();
String? email = pref.getString("useremail");
String? token = pref.getString('token');
String date = DateFormat("yyyy-MM-dd").format(DateTime.now());
String time = DateFormat("Hms").format(DateTime.now());
print(time);
print(date);
dynamic response =
await http.post(Uri.parse(apiURl + "/api/user-log/"), body: {
'user': email,
'start_time': time,
'start_date': date,
});`
Here in the documentation you can found the following snippet, where the headers are sent with the HTTP request:
Future<http.Response> createAlbum(String title) {
return http.post(
Uri.parse('https://jsonplaceholder.typicode.com/albums'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'title': title,
}),
);
}
Add header in the post method as below:
var headers = {
'authorization': token,
"Accept": "application/json"
};
dynamic response =
await http.post(Uri.parse(apiURl + "/api/user-log/"),headers: headers), body: {
...
});
final response = await http.get(
Uri.parse(apiURl + "/api/user-log/"),
body: {}
headers: {
HttpHeaders.authorizationHeader: "<Your token here>",
},
);
Documentation here

GET request is showing "406 - Not Acceptable" in Dart for http package Flutter

I am requesting the following Get request but getting "406 - Not Acceptable" response. Here I have attached the request code:
final Map<String, String> tokenData = {"Authorization": token};
var response = await http.get(url, headers: tokenData);
if (response.statusCode == 200) {
var jsonResponse = convert.jsonDecode(response.body);
var itemCount = jsonResponse['totalItems'];
print('Number of books about http: $itemCount.');
} else {
print('Request failed with status: ${response.statusCode}.');
print('Request failed with reason: ${response.reasonPhrase}.');
}
Change your tokenData as follow:
final Map<String, String> tokenData = {
'Authorization': token
'Content-type': 'application/json',
'Accept': 'application/json',
};
It will work if you are getting JSON as a response.

Http Post in Flutter not Sending Headers

I am trying to do a post request in Flutter
Below is the code
final String url = Urls.HOME_URL;
String p = 'Bearer $vAuthToken';
final Map<String, String> tokenData = {
"Content-Type": "application/x-www-form-urlencoded",
'Vauthtoken': p
};
final Map<String, String> data = {
'classId': '4',
'studentId': '5'
};
final response = await http.post(Uri.parse(url),
headers: tokenData,
body: jsonEncode(data),
encoding: Encoding.getByName("utf-8"));
if (response.statusCode == 200) {
print(response.body);
} else {
print(response.body);
}
However the header data is not working. I have the correct data and everything. This request works perfectly when done in Postman Client.
Any one has any idea what is wrong?
Any suggestion is appreciated.
Thanks

How to send parameters in headers using http package in flutter

I want to sent my auth-key in headers using http package but unfortunately its not working kindly help me .
var url = "https://paysafemoney.com/psmApi/Psm/userDashboard";
var response = await http.post(
url,
headers: {
"auth-key": LoginConfirmActivity.authKey,
},
body: sendLoginData,
);
print("Response = ${response.body}");
You can do like this
var fullUrl = '$stripeBaseUrl$customerId/sources?source=$cardToken';
var header = {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': stripeKey,
};
final response = await client.post(fullUrl, headers: header);