Just learning Flutter and running into this issue when trying to call an API:
final response = await http.get(
Uri.https(apiBaseUrl, apiBaseEndpoint + "/tasks"),
headers: {
"Authorization": "Bearer " + apiKey,
},
);
print(response.body);
Part of my response contains Ä°ftar and it's supposed to be İftar. I imagine it's some encoding problem? curl gives me back the response with the proper characters.
Basically: is this a text encoding problem? If so, how do I fix my request?
Ok, after a little bit more digging on the http docs I realized it wasn't in how I made my request that needed to change, but how I handled the response. I was doing
final decodedJson = json.decode(response.body);
and I should've been doing:
final decodedJson = json.decode(utf8.decode(response.bodyBytes));
That has solved my issue!
Related
I am making a mobile app using Flutter & Dart. I have a put request that updated a database. The request keeps giving a bad request 400. The code is as follow:
Future<void> updateStudent(int id, Student newStudent) async {
var res = await http.put(
Uri.parse('https://10.0.2.2:7030/api/Student/{id}?Id=7'),
headers: {
"Accept": "application/json",
"content-type": "application/json"
},
body: json.encode({
'id': newStudent.id,
'dep_id': newStudent.depId,
'name_ar': newStudent.nameAr,
'name_en': newStudent.nameEn,
'name_moth': newStudent.nameMoth,
'birth': newStudent.birth,
}));
}
I tried many suggestions. Also I tried to do the request this way code. But same issue.
The API is working just fine with postman but for some reason it does not with Flutter. I postman I used query params and body with raw json There is something wrong but I am not able to find it.
Any idea or suggestions?
Update
I debugged the request and it showing an error on "BodyField" stating the Bad state: Cannot access body fields of a Request without "content-type : application/x-www-form-urlencoded
Even though I am using json.
I also tried to change the content type but it is giving unsupported media type.
in postman I am using put method with this url
https://localhost:7030/api/Student/{id}?Id=7
Change your url and body to this:
var url = Uri.https('https://10.0.2.2:7030', 'api/Student/{id}');
var res = await http.put(
url,
headers: {
"Accept": "application/json",
"content-type": "application/json"
},
body: {
'id': newStudent.id,
'dep_id': newStudent.depId,
'name_ar': newStudent.nameAr,
'name_en': newStudent.nameEn,
'name_moth': newStudent.nameMoth,
'birth': newStudent.birth,
});
you already add id in body, no need to put it in url too.
I have a get request in flutter, the response body comes as json, is there any way to have it in XML?
In the Swagger UI it comes as XML but I don't know why it comes as Json in my request.
I am using this lines:
final response = await http.get(Uri.parse(selectedURL));
print(response.body);
You probably need to set the Accept header to something like:
Accept: application/xml
Something like this might do the trick:
final response = await http.get(
Uri.parse(selectedURL),
headers: {
'Accept': 'application/xml',
},
);
print(response.body);
does anyone have any idea what could be wrong because I have no idea what could be causing the error?
When I'm connecting with my Rest Api where I have to put some String data and also 1 File by PostMan like in picture below:
https://files.fm/f/vrxe9zsnu
it all works how it shoud. I mean that it create new order in DB with BLOB and datas but then I'm trying doing this same on Mobile App project Idk why I'm getting respone status code 208 with message
{"timestamp":1647283779156,"message":"could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement"}
I'm sending there File which I get from ImagePicker library.
Future<String> multipart(File imageFile) async {
var request = http.MultipartRequest(
'POST', Uri.parse(ApiHeadersInfo().createNewOrderUrl));
request.headers.addAll({
"Authorization": "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJwcEBnbWFpbC5jb20iLCJleHAiOjE2NDgyODIxNjR9.ZYOo2g1iexKZynpdoIkCMbsxzHhwLDNZEIj9qUlZj2GcwqLtUTcxaBS49_xPkewg-i22eKkGNjIQAMtjw5vwIQ",
"Content-type": "multipart/form-data"
});
request.files.add(await http.MultipartFile.fromPath('multipartFile', imageFile.path));
request.fields['customer'] = "newOrder.customer";
request.fields['loadingCompanyName'] = "newOrder.loadingCompanyName";
request.fields['loadingCity'] = "newOrder.loadingCity";
request.fields['loadingPostcode'] = "newOrder.loadingPostcode";
request.fields['dateOfLoading'] = "newOrder.dateOfLoading";
request.fields['unloadingCompanyName'] = "newOrder.unloadingCompanyName";
request.fields['unloadingPostcode'] = "newOrder.unloadingPostcode";
request.fields['dateOfUnloading'] = "newOrder.dateOfUnloading";
request.fields['nettoPrice'] = '11.0';
request.fields['bruttoPrice'] = '11.0';
request.fields['information'] = "newOrder.information";
http.Response response = await http.Response.fromStream(await request.send());
print("Result: ${response.statusCode}");
print("fields: ${request.fields}");
return response.body;
}
Any ideas what is wrong here? Maybe bad http request method I just use for it ?
Have completely no idea.
Many thanks.
I have just updated to Flutter 2. I updated all the dependencies including http. Now I have a problem with the following:
Future<UserLogin> fetchStaff(String pUserName, String pPassword) async {
final response = await http
.post(Uri.encodeFull('$kBaseUrl/LoginService/CheckLogin'),
headers: {'Content-Type': 'application/json', 'Accept': 'application/json'},
body: '{ "pUser": "$pUserName", "pPassword": "$pPassword"}')
.timeout(Duration(seconds: kTimeOutDuration));
I'm getting an error on the: Uri.encodeFull('$kBaseUrl/LoginService/CheckLogin'
"The argument type 'String' can't be assigned to the parameter type 'Uri'."
$kBaseUrl = 'https://subdom.mydomain.com:443/mobile';
What do I need to change?
Post method used to take string previously, now they have modified to Uri
so used, Uri.parse to get the uri
.post(Uri.parse(Uri.encodeFull('$kBaseUrl/LoginService/CheckLogin'))
Like the error says, this is because post function expects a Uri and you have passed a String (returned by Uri.encodeFull) to it. You need to use Uri.parse to pass a Uri to it.
final response = await http
.post(Uri.parse('$kBaseUrl/LoginService/CheckLogin'),
If you had a String, such as:
String strUri="${yourBaseUrl}/yourPath/yourFile";
Using
http.post(Uri.parse(strUri),
headers: {...},
body: {...},
);
is enough to get all working back.
You may try Uri.tryParse(strUri) to handle null if the uri string is not valid as a URI or URI reference.
I hope to use nutritionix api to get food information for the users of my application, I manage to get the call to work in Postman, however I cannot convert it to dart code. I am getting this error: '{message: Unexpected token " in JSON at position 0}'
Here is my (POST) postman call:
Here is my attempt at converting that to dart code:
Future<void> fetchNutritionix() async {
String url = 'https://trackapi.nutritionix.com/v2/natural/nutrients';
Map<String, String> headers = {
"Content-Type": "application/json",
"x-app-id": "5bf----",
"x-app-key": "c3c528f3a0c68-------------",
"x-remote-user-id": "0",
};
String query = 'query: chicken noodle soup';
http.Response response =
await http.post(url, headers: headers, body: query);
int statusCode = response.statusCode;
print('This is the statuscode: $statusCode');
final responseJson = json.decode(response.body);
print(responseJson);
//print('This is the API response: $responseJson');
}
Any help would be appreciated! And, again thank you!
Your postman screenshot shows x-www-form-urlencoded as the content-type, so why are you changing that to application/json in your headers? Remove the content type header (the package will add it for you) and simply pass a map to the body parameter:
var response = await http.post(
url,
headers: headers,
body: {
'query': 'chicken soup',
'brand': 'acme',
},
);
Also you can now generate Dart code (and many other languages) for your Postman request by clicking the Code button just below the Save button.
click the three dotes button in request tab and select code option then select your language that you want convert code to
review the query you're posting
your Postman input is x-www-form-urlencoded instead of plain text
String query = 'query: chicken noodle soup';
why don't you try JSON better
String query = '{ "query" : "chicken noodle soup" }';