Send multiple files in json body - json

I am new in flutter, trying to send multiple files in single request but failing miserably. I have attached the json body parameter screenshot also.
I have to send an array of multiple pdf files along with other parameters.
qualifications:{"qualifications": [{"id": "25", "qualification": "nurse", "grade": 11, "date": "2020-07-20", "proof": "file"}]}
nurse_id:26
This is what i did
List<Map> _jsonArray = new List();
Map<String, String> arrayBody = {
'id': '',
'qualification': 'nurse',
'grade': '13',
'date': _dateController.text,
};
List<http.MultipartFile> _mfiles = new List();
_jsonArray.add(arrayBody);
var body = json.encode({'qualifications':_jsonArray });
_mfiles.add( await http.MultipartFile.fromPath(
'proof',
_file
),
);
_mfiles.add( await http.MultipartFile.fromPath(
'qt_statement',
_qtStatement
),);
final Map<String, String> requestheaders = {
'Authorization': 'Bearer '+token_id,
};
var request = http.MultipartRequest('POST', Uri.parse(Urls.PERSONAL_DETAILS));
request.headers.addAll(requestheaders);
request.files.addAll(_mfiles);
request.fields[Constants.nurse_id] = nurse_id;
var res = await request.send();
var content = StringBuffer();
if(res.statusCode == 200){
res.stream.transform(utf8.decoder).listen((value) {
content.write(value);
},
onDone: (){
var data = json.decode(content.toString());
print(data);
});
}
The json i am sending looks like this
{"qualifications":[{"id":"","qualification":"nurse","grade":"13","date":"2020-07-31"}]}
Here 'proof' key is missing. Hence, getting 'Proof cannot be null' as response from server.
Struggling with this issue for more than 3 hours. Any suggestion or help will be highly appreciated

i assume your server expects the request to have a field called proof which contains the file data as part of the json post data. In this case, you can encode the file content (bytes) as base64 and set it inside your json data.
something like:
string proof = read_file_as_base64(filepath); //you need to read and encode the file content here.
Map<String, String> arrayBody = {
'id': '',
'qualification': 'nurse',
'grade': '13',
'date': _dateController.text,
'proof': proof
};

Related

Store a api response data in firebase collections using flutter

So, I have been making a post request to a REST API and I want to store the response data in the firebase cloud store collection.
What I have done so far:
I have created the model class for the response data and have written a function that will make this post-call.
I am not getting any such error but still, neither the response is getting printed in the console nor the data is being uploaded in the firebase.
Also, I have checked with almost all the StackOverflow questions that relate to my kind of problem.
Herewith I am attaching my code snippets:
Function:
//This function is only not getting called I don't know why.
final List<KycDetails> _kyc = [];
Dio dio = Dio();
TextEditingController aadhar = TextEditingController();
Future<List<KycDetails>> postData() async {
const String pathUrl = 'https://jsonplaceholder.typicode.com/posts';
dynamic data = {'title': aadhar.text, 'body': 'Flutter', 'userId': 1};
List<KycDetails> details = [];
var response = await dio.post(pathUrl,
data: data,
options: Options(
headers: {'Content-Type': 'application/json; charset=UTF-8'}));
if (response.statusCode == 200) {
print('ok');
var urjson = jsonDecode(response.data);
for (var jsondata in urjson) {
details.add(KycDetails.fromJson(jsondata));
}
}
return details;
}
Widget where I am calling the function and storing the data in firebase
InkWell(
hoverColor: Colors.red,
onTap: () async {
print('API CALLING');
await postData().then((value) {
setState(() {
_kyc.addAll(value);
});
print(value);
});
Map<String, String> data = {
"aadhar": aadhar.text,
"title": _kyc[0].title,
"userId": _kyc[0].userId.toString(),
};
FirebaseFirestore.instance.collection('kyc').add(data);
},
child: const Text('Submit'),
),
API response data:
{"title": "resume", "body": "Flutter", "userId": 1, "id": 101}
Model Class:
class KycDetails {
KycDetails({
required this.title,
required this.body,
required this.userId,
required this.id,
});
String title;
String body;
int userId;
int id;
factory KycDetails.fromJson(Map<String, dynamic> json) => KycDetails(
title: json["title"],
body: json["body"],
userId: json["userId"],
id: json["id"],
);
Map<String, dynamic> toJson() => {
"title": title,
"body": body,
"userId": userId,
"id": id,
};
}
I hope I have provided you with the necessary information
Am stuck on this problem for quite a few days Would appreciate it if anyone can solve my problem considering my code.
For starters, when you make a post request the success code you're looking for is 201 indicating that a resource has been successfully created.
So nothing in this code block will run.
if (response.statusCode == 200) {
print('ok');
var urjson = jsonDecode(response.data);
for (var jsondata in urjson) {
details.add(KycDetails.fromJson(jsondata));
}
}
response.data doesn't need jsonDecode here. It returns in the form of a map so you can cast it as such.
So this
var urjson = jsonDecode(response.data);
can be this
final urjson = response.data as Map<String, dynamic>;
As for this line
for (var jsondata in urjson) {
details.add(KycDetails.fromJson(jsondata));
}
The response is a single map, not a list. That single map is in the form of your KycDetails model so you don't need to loop through anything.
So you can create your object with your fromJson method.
final kycDetail = KycDetails.fromJson(urjson);
Then you can just do this to add a properly initiated KycDetails object to the list.
details.add(kycDetail);
If all you're trying to do is add a single object to Firebase then none of this in your onTap is necessary. Also trying to access the property at index 0 will not be the most recent addition to the list. You'd need to add the index of the last item in the list.
Map<String, String> data = {
"aadhar": aadhar.text,
"title": _kyc[0].title,
"userId": _kyc[0].userId.toString(),
};
FirebaseFirestore.instance.collection('kyc').add(data);
You can just add to Firebase from your postData function.
if (response.statusCode == 201) {
print('ok');
final urjson = response.data as Map<String, dynamic>;
final kycDetail = KycDetails.fromJson(urjson);
details.add(kycDetail);
FirebaseFirestore.instance.collection('kyc').add(kycDetail.toJson());
}

Unable to post MAP values to https post in flutter

I have made a separate class which is my model class and has a function that posts to DISCORD WEB HOOK. I am setting the map values to true from the FORM UI screen. The textformfield value gets POSTED while MAP gives an error as following, just the textfield value gets posted and works fine but MAP doesn't work. Am I writing bad JSON?
P.S it is a discord hook so "content" is neccessary
I/flutter (26346): {"content": ["Could not interpret \"['testing options', {'Spot': True, 'Red': True, 'Thich': False, 'Dry': True, 'Strech': False}]\" as string."]}
Here is the code for the class
import 'dart:convert';
import 'package:http/http.dart';
class DataPush {
static const String Spot = 'Spot';
static const String Red = 'Red';
static const String Thick = 'Thich';
static const String Dry = 'Dry';
static const String Strech = 'Strech';
String firstName = '';
Map<String, bool> passions = {
Spot: false,
Red: false,
Thick: false,
Dry: false,
Strech: false,
};
save() {
print("Saved");
}
makePostRequest() async {
final uri = Uri.parse(
'MY DISCORD WEBHOOK URL');
final header = {'Content-Type': 'application/json'};
Map<String, dynamic> body = {
"content": [firstName, passions]
};
String jsonBody = json.encode(body);
Response response = await post(
uri,
headers: header,
body: jsonBody,
);
int statusCode = response.statusCode;
String responseBody = response.body;
print(statusCode);
print(responseBody);
}
}
Remove this line and directly pass Map to post function.
String jsonBody = json.encode(body);
The Discord webhook documentation specifies the content field as a string, but you are sending an array as the value of that field - If you really want to send an array in the content field, you'll need to encode it as a json string itself first.
makePostRequest() async {
final uri = Uri.parse(
'MY DISCORD WEBHOOK URL');
final header = {'Content-Type': 'application/json'};
Map<String, dynamic> body = {
"content": json.encode([firstName, passions]) // Change this line
};
String jsonBody = json.encode(body);
Response response = await post(
uri,
headers: header,
body: jsonBody,
);
int statusCode = response.statusCode;
String responseBody = response.body;
print(statusCode);
print(responseBody);
}

Flutter json.decode() sometimes works and sometimes doesnt while parsing the same object

I'm new to flutter and was working with json. when I am decoding the response I got from the server by using json.decode() I sometimes get the following error
flutter: FormatException: Unexpected character (at character 21)
sometimes it works perfectly.
this is my code
try {
Map<String, dynamic> map = new Map<String, dynamic>.from(
json.decode(contents));
if (map["CompletedJobPackages"] != null) {
DataStream.compleatedJobspackage =
DataStream.parseCompletedJobs(map["CompletedJobPackages"]);
print(map["CompletedJobPackages"]);
compleatedJobs = DataStream.compleatedJobspackage;
}
CompletedJobloaded = true;
}
catch(e){
print(e);
ToastUtils.showCustomToast(context, "An Error Occured. Try Again !", false);
}
this is the object I'm trying to decode
{
"CompletedJob": {
"CompletedJobID": 7,
"DriverID": 34,
"JobNumber": "80252C20",
"TraderID": 7,
"TripType": "Two Way",
"CargoType": "wd",
"CargoWeight": 230,
"LoadingPlace": "dc",
"UnloadingPlace": "sd",
"LoadingDate": "2020-05-25",
"LoadingTime": "12:59:00",
"EntryExit": 1,
"AcceptedDelay": 4,
"Price": 34,
"Created": "2020-05-23T04:22:10.000Z"
},
"BillPaid": 0,
"DriverReview": {
"DriverReviewID": 11,
"DriverID": 34,
"TraderID": 7,
"CompletedJobID": 7,
"Rating": 100,
"Review": "dscsdc",
"Created": "2020-05-28"
}
},
Okay so after doing a little research I realized the problem.
the HTTP library I was using dart:io was not returning the complete JSON string in the response thus their was a problem in converting it into a JSON object
I just used
import 'package:http/http.dart' as http;
Map<String, String> requestHeaders = {
'Content-type': 'application/json',
'Accept': 'application/json',
'Authorization':"JWT "+DataStream.token
};
final response = await http.get(URLs.getCompletedJobPackagesURL(), headers:requestHeaders);
if (response.statusCode == 200) {
var jsonResponse = convert.jsonDecode(response.body);
print(jsonResponse);
Map<String, dynamic> map = convert.jsonDecode(response.body);
if(map["CompletedJobPackages"]!= null) {
DataStream.compleatedJobspackage =
DataStream.parseCompletedJobs(map["CompletedJobPackages"]);
compleatedJobs = DataStream.compleatedJobspackage;
}
setState(() {
});
}
instead of
import 'dart:io';
final client = HttpClient();
try{
final request = await client.getUrl(Uri.parse(URLs.getDrivingLicenceURL()));
request.headers.set(HttpHeaders.contentTypeHeader, "application/json; charset=UTF-8");
request.headers.add("Authorization", "JWT "+DataStream.token);
final response = await request.close();
response.transform(utf8.decoder).listen((contents) async {
// print(response.statusCode);
Map<String, dynamic> driverMap = jsonDecode(contents) as Map<String, dynamic>;
isloadlicence = true;
if(driverMap["CompletedJobPackages"]!= null) {
DataStream.compleatedJobspackage =
new DrivingLicence.fromJson(driverMap["CompletedJobPackages"]);
}
setState(() {
});
});
}catch(e){
print(e);
ToastUtils.showCustomToast(context, "An Error Occurred. Try Again !", false);
//pr.hide();
}
Definitely the problem is the emulator, after several days looking for the problem I found this post and I have verified that it does not fail on my real phone.
Different emulators fails different number of times (maybe something related to the ram??).
Also consider using compute(jsonDecode, response.body) to decode the string as indicated in Parse JSON in the background.
compute fails less times on emulators, but still failing sometimes. Doesn't fails in real phone.

how to post json array on flutter

I have some problem to post json array with flutter.
When I hit api with postman using json it works. Screenshot postman:
Since I know on body just accept Map <String,String> CMIIW
so i turn body into like this
List<Map<String,String>> products = [
{
"product_id": "1",
"buy_quantity": "1",
"product_price": "1000",
"is_voucher": "0",
},
{
"product_id": "2",
"buy_quantity": "2",
"product_price": "2000",
"is_voucher": "0",
},
];
final String jsonProduct = json.encode(products);// here im trying to
Map<String,String> _body = {
"buyer_id": '',
"buyer_firstname": postCart.buyerFirstname,
"phone_number": postCart.phoneNumber,
"transaction_total_price": postCart.transactionTotalPrice.toString(),
"voucher_id": 0.toString(),
"voucher_code": 0.toString(),
"payment_id": postCart.paymentId.toString(),
"payment_name": postCart.paymentName,
"products" : jsonProduct
};
but i still got error,
thanks!
I'm assuming that you are using the http package.
This is an example of how to make an HTTP POST request with a json payload on the body:
Future<Lead> createLead(String clientName, String clientEmail, String clientPhone, String eventId) async {
// Create storage
final storage = new FlutterSecureStorage();
// Get API url from env
String url = (DotEnv().env['BASE_URL'] + "/leads/create");
String authToken = await storage.read(key: 'api-token');
// Create some request headers
Map<String, String> requestHeaders = {
'Content-type': 'application/json',
'Accept': 'application/json',
'X-Token': authToken
};
final response = await http.post(
url,
// enconde some JSON data on the request body
body: json.encode(
{
'event_id': eventId,
'name': clientName,
'phone': clientPhone,
'email': clientEmail
}
),
headers: requestHeaders
);
if (response.statusCode == 200) {
final leadsJson = json.decode(response.body);
Lead lead = Lead.fromJson(leadsJson);
return lead;
} else {
// If that response was not OK, throw an error.
// throw Exception('Failed to load post');
return null;
}
}
Hope it helps.
If you want to achieve the JSON in the screen shot you need to make several changes.
Notice that the list of products shows unquoted integers when converted to JSON, so these need to be kept as ints in your Dart class. (Note the preferred syntax, too.)
var products = <Map<String, int>>[
{
'product_id': 1,
'buy_quantity': 1,
'product_price': 1000,
'is_voucher': 0,
},
{
'product_id': 2,
'buy_quantity': 2,
'product_price': 2000,
'is_voucher': 0,
},
];
The type of _body needs to change because it now contains a list as well as strings. (Keep the rest the same - like the toStrings - as your screenshot shows that even the integers are quoted as strings.)
var _body = <String, dynamic>{
'buyer_id': '',
'buyer_firstname': postCart.buyerFirstname,
'phone_number': postCart.phoneNumber,
'transaction_total_price': postCart.transactionTotalPrice.toString(),
'voucher_id': 0.toString(),
'voucher_code': 0.toString(),
'payment_id': postCart.paymentId.toString(),
'payment_name': postCart.paymentName,
'products': products
};
Lastly json and (preferably) utf8 encode the resulting map, and pass that as the body parameter of your http.post.
var bytes = utf8.encode(json.encode(_body));
await http.post(url, body: bytes, headers: someHeaders);
One of your headers could be content-type: application/json;encoding=utf-8 to tell the server that you are sending UTF8 encoded JSON.

Flutter: Send JSON body for Http GET request

I need to make a GET request to an API from my Flutter app which requires request body as JSON (raw).
I tested the API with JSON request body in Postman and it seems to be working fine.
Now on my Flutter application I am trying to do the same thing:
_fetchDoctorAvailability() async {
var params = {
"doctor_id": "DOC000506",
"date_range": "25/03/2019-25/03/2019" ,
"clinic_id":"LAD000404"
};
Uri uri = Uri.parse("http://theapiiamcalling:8000");
uri.replace(queryParameters: params);
var response = await http.get(uri, headers: {
"Authorization": Constants.APPOINTMENT_TEST_AUTHORIZATION_KEY,
HttpHeaders.contentTypeHeader: "application/json",
"callMethod" : "DOCTOR_AVAILABILITY"
});
print('---- status code: ${response.statusCode}');
var jsonData = json.decode(response.body);
print('---- slot: ${jsonData}');
}
However the API gives me an error saying
{message: Missing input json., status: false}
How do I send a raw (or rather JSON) request body for Http GET request in Flutter?
GET
GET requests are not intended for sending data to the server (but see this). That's why the http.dart get method doesn't have a body parameter. However, when you want to specify what you are getting from the server, sometimes you need to include query parameters, which is a form of data. The query parameters are key-value pairs, so you can include them as a map like this:
final queryParameters = {
'name': 'Bob',
'age': '87',
};
final uri = Uri.http('www.example.com', '/path', queryParameters);
final headers = {HttpHeaders.contentTypeHeader: 'application/json'};
final response = await http.get(uri, headers: headers);
POST
Unlike GET requests, POST requests are intended for sending data in the body. You can do it like this:
final body = {
'name': 'Bob',
'age': '87',
};
final jsonString = json.encode(body);
final uri = Uri.http('www.example.com', '/path');
final headers = {HttpHeaders.contentTypeHeader: 'application/json'};
final response = await http.post(uri, headers: headers, body: jsonString);
Note that the parameters were a Map on the Dart side. Then they were converted to a JSON string by the json.encode() function from the dart:convert library. That string is the POST body.
So if the server is asking you to pass it data in a GET request body, check again. While it is possible to design a server in this way, it isn't standard.
uri.replace... returns a new Uri, so you have to assign it into a new variable or use directly into the get function.
final newURI = uri.replace(queryParameters: params);
var response = await http.get(newURI, headers: {
"Authorization": Constants.APPOINTMENT_TEST_AUTHORIZATION_KEY,
HttpHeaders.contentTypeHeader: "application/json",
"callMethod" : "DOCTOR_AVAILABILITY"
});
using post:
var params = {
"doctor_id": "DOC000506",
"date_range": "25/03/2019-25/03/2019" ,
"clinic_id":"LAD000404"
};
var response = await http.post("http://theapiiamcalling:8000",
body: json.encode(params)
,headers: {
"Authorization": Constants.APPOINTMENT_TEST_AUTHORIZATION_KEY,
HttpHeaders.contentTypeHeader: "application/json",
"callMethod" : "DOCTOR_AVAILABILITY"
});
You can use Request class as following:
var request = http.Request(
'GET',
Uri.parse("http://theapiiamcalling:8000"),
)..headers.addAll({
"Authorization": Constants.APPOINTMENT_TEST_AUTHORIZATION_KEY,
HttpHeaders.contentTypeHeader: "application/json",
"callMethod": "DOCTOR_AVAILABILITY",
});
var params = {
"doctor_id": "DOC000506",
"date_range": "25/03/2019-25/03/2019",
"clinic_id": "LAD000404"
};
request.body = jsonEncode(params);
http.StreamedResponse response = await request.send();
print(response.statusCode);
print(await response.stream.bytesToString());
Also, note that Postman that can convert an API request into a code snippet in more than 15 languages. If you select Dart, you will find a similar code to above.
It may help someone those who used Getx for api integration . We can use request method for these kind of requirement.
Map<String, dynamic> requestBody = { 'id' : 1};
Response<Map<String, dynamic>> response =
await request(url, 'get', body: requestBody);
if you want to send complex/nested data through a GET request like the sample below, you can use a simple class i created on github
https://github.com/opatajoshua/SimplifiedUri
final params = {
'name': 'John',
'columns': ['firstName', 'lastName'],
'ageRange': {
'from': 12,
'to': 60,
},
'someInnerArray': [1,2,3,5]
};
final Uri uri = SimplifiedUri.uri('http://api.mysite.com/users', params);
final headers = {HttpHeaders.contentTypeHeader: 'application/json'};
final response = await http.get(uri, headers: headers);
output
http://api.mysite.com/users?name=John&columns%5B%5D=firstName&columns%5B%5D=lastName&ageRange%5Bfrom%5D=12&ageRange%5Bto%5D=60&someInnerArray%5B%5D=1&someInnerArray%5B%5D=2&someInnerArray%5B%5D=3&someInnerArray%5B%5D=5