Is there any way to convert the response body to XML in flutter? - flutter

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

Related

Why put flutter returning bad request?

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.

Dart character encoding in http request

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!

http.get causes Japanese text to get jumbled

I am using following code to get data from a server that has Japanese text.
final response = await http.get(url, headers: {
'Content-Type': 'application/json',
});
I get the data correctly but all the Japanese text does not look correct. They look like
æ±äº¬éƒ½ä¸­å¤®åŒºæ—¥æœ¬æ©‹å®¤ç”º3-3-9
When I use "Postman" to check the data, it is displaying Japanese text correctly.
How do I get around this issue?
I have the same problem when I put the values.
I use;
Map<String, String> headers = {
'Content-Type': 'application/json',
'authorization': basicAuth,
};
final msg = jsonEncode(data);
final response = await http.put(
url,
headers: headers,
body: msg,
);
The data does not seems to encode correctly, when they are in Japanese.
Can someone provide some help on this ?

Converting my Postman Call to Dart/Flutter API Call

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

Dio's get request returning incomplete data

I'm making a get request with the Dio library (https://pub.dev/packages/dio), but apparently it is not getting all the data in the coming json.
Here's the request:
This is the json that comes from it:
But this is the Response object I get from this request:
:
Note how the "headers" field in the json has substantially more values than my headers field in response.data.
Am I doing something wrong here?
You are returning response without extract it's content. That's OK, but you need to extract the body from response where you are calling this method:
http.Response response = await _yourApi.getData();
if (response.statusCode == 200) {
//Do what you want with body
final body = response.body;
}