我想使用 API 在我的 flutter 应用程序中使用 convertapi.com 将 word 文件转换为 pdf 文件,我的 POST 方法有什么问题
I want to use API for convert word file to pdf file in my flutter app using convertapi.com ,what is wrong with my POST method
import 'dart:io';
import 'package:aconvert/pages/view.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'dart:convert';
import 'dart:typed_data';
import 'package:aconvert/services/api.dart';
import 'package:path_provider/path_provider.dart';
import 'package:http/http.dart' as http;
import 'package:pdf_viewer_plugin/pdf_viewer_plugin.dart';
//final clientId = '2720faeb-aa0f-46ee-aa0a-8bf7559268fc';
//final secret = '65cc9d8e63d2a01be5dd5abde8cbc970';
//var configuration = Configuration(secret, clientId);
class Docx extends StatefulWidget {
const Docx({Key? key}) : super(key: key);
@override
_DocxState createState() => _DocxState();
}
class _DocxState extends State<Docx> {
//view pdf
String? docFilePath;
String? docFilename;
String? docFiledata;
File? _file;
late final pathController = TextEditingController();
pick_file() async{
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['doc','txt','TXT','png','jpg','pdf','PDF','rtf'],
);
if (result != null) {
// List<File> files = result.paths.map((path) => File(path!)).toList();
File file = File(result.files.single.path!);
_file = File(result.files.single.path!);
setState(() {
docFilePath =result.files.single.path!;
docFilename = _file?.path.split('/').last;
Uint8List? fileInByte = _file?.readAsBytesSync();
docFiledata = base64Encode(fileInByte!);
print("******** $docFilename **********");
});
} else {
// User canceled the picker
}
}
// convert to pdf api
Future postfile() async{
String url = 'https://v2.convertapi.com/convert/doc/to/pdf?Secret=5Wq3HMYGVzqViAXJ&Token=636106935&StoreFile=true';
final Uri uri = Uri.parse(url);
http.Response response = await http.post(uri,body: {
"Parameters": [
{
"Name": "File",
"FileValue": {
"Name": docFilename,
"Data": docFiledata
}
},
{
"Name": "StoreFile",
"Value": true
}
]
} , headers: {
"Content-Type": "application/json"
});
var responsebody = json.decode(response.body) ;
print('Response status: ${response.statusCode}');
print('------------------------------------');
print('Response body: ${response.body}');
print('------------------------------------');
print('Response body: ${responsebody}');
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('from docx to pdf'),
titleTextStyle: TextStyle(color: Colors.white70 , fontSize: 25 , fontWeight: FontWeight.bold),
backgroundColor: Colors.deepOrangeAccent,
elevation: 0,
centerTitle: true,
),
body: SingleChildScrollView(
child:Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(height: 20,),
InkWell(
onTap: pick_file,
child: Container(
height: 60,
padding: EdgeInsets.all(10),
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.redAccent,
),
child: Center(child: Text('select file ' ,style: TextStyle(fontSize: 30,color: Colors.white), ))),
),
SizedBox(height: 20,),
InkWell(
onTap: (){
Get.to(()=>View(docFilePath!));
},
child: Container(
height: 60,
padding: EdgeInsets.all(10),
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.redAccent,
),
child: Center(child: Text('show selected file' ,style: TextStyle(fontSize: 30,color: Colors.white), ))),
),
SizedBox(height: 20,),
//convert
InkWell(
onTap: postfile,
child: Container(
height: 60,
padding: EdgeInsets.all(10),
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.redAccent,
),
child: Center(child: Text('convert' ,style: TextStyle(fontSize: 30,color: Colors.white), ))),
),
SizedBox(height: 20,),
InkWell(
onTap: (){},
child: Container(
height: 60,
padding: EdgeInsets.all(10),
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.redAccent,
),
child: Center(child: Text('upload to cloud' ,style: TextStyle(fontSize: 30,color: Colors.white), ))),
),
],
),
),
);
}
}
E/flutter (11254): [错误:flutter/lib/ui/ui_dart_state.cc(209)] 未处理的异常:错误状态:无法设置内容类型为“[=19”的请求的正文字段=]”。
E/flutter (11254): #0 Request.bodyFields= (包:http/src/request.dart:133:7)
E/flutter (11254): #1 BaseClient._sendUnstreamed (包:http/src/base_client.dart:87:17)
E/flutter (11254): #2 BaseClient.post (包:http/src/base_client.dart:32:7)
E/flutter (11254): #3 post。 (包:http/http.dart:69:16)
E/flutter (11254): #4 _withClient (包:http/http.dart:164:20)
E/flutter (11254): #5 post (包:http/http.dart:68:5)
E/flutter (11254): #6 _DocxState.postfile (包:aconvert/pages/fromdocxtopdf.dart:61:40)
E/flutter (11254): #7 _InkResponseState._handleTap (包:flutter/src/material/ink_well.dart:989:21)
E/flutter (11254): #8 GestureRecognizer.invokeCallback (包:flutter/src/gestures/recognizer.dart:198:24)
E/flutter (11254): #9 TapGestureRecognizer.handleTapUp (包:flutter/src/gestures/tap.dart:608:11)
E/flutter (11254): #10 BaseTapGestureRecognizer._checkUp (包:flutter/src/gestures/tap.dart:296:5)
E/flutter (11254): #11 BaseTapGestureRecognizer.handlePrimaryPointer (包:flutter/src/gestures/tap.dart:230:7)
E/flutter (11254): #12 PrimaryPointerGestureRecognizer.handleEvent (包:flutter/src/gestures/recognizer.dart:563:9)
E/flutter (11254): #13 PointerRouter._dispatch (包:flutter/src/gestures/pointer_router.dart:94:12)
E/flutter (11254): #14 PointerRouter._dispatchEventToRoutes。 (包:flutter/src/gestures/pointer_router.dart:139:9)
E/flutter (11254): #15 _LinkedHashMapMixin.forEach (飞镖:collection-patch/compact_hash.飞镖:539:8)
E/flutter (11254): #16 PointerRouter._dispatchEventToRoutes (包:flutter/src/gestures/pointer_router.dart:137:18)
E/flutter (11254): #17 PointerRouter.route (包:flutter/src/gestures/pointer_router.dart:123:7)
E/flutter (11254): #18 GestureBinding.handleEvent (包:flutter/src/gestures/binding.dart:439:19)
E/flutter (11254): #19 GestureBinding.dispatchEvent (包:flutter/src/gestures/binding.dart:419:22)
E/flutter (11254): #20 RendererBinding.dispatchEvent (包:flutter/src/rendering/binding.dart:322:11)
E/flutter (11254): #21 GestureBinding._handlePointerEventImmediately (包:flutter/src/gestures/binding.dart:374:7)
E/flutter (11254): #22 GestureBinding.handlePointerEvent (包:flutter/src/gestures/binding.dart:338:5)
E/flutter (11254): #23 GestureBinding._flushPointerEventQueue (包:flutter/src/gestures/binding.dart:296:7)
E/flutter (11254): #24 GestureBinding._handlePointerDataPacket (包:flutter/src/gestures/binding.dart:279:7)
E/flutter (11254): #25 _rootRunUnary (飞镖:async/zone.dart:1442:13)
E/flutter (11254): #26 _CustomZone.runUnary (飞镖:async/zone.飞镖:1335:19)
E/flutter (11254): #27 _CustomZone.runUnary 守卫 (dart:async/zone.dart:1244:7)
E/flutter (11254): #28 _invoke1 (飞镖:ui/hooks.飞镖:170:10)
E/flutter (11254): #29 PlatformDispatcher._dispatchPointerDataPacket (飞镖:ui/platform_dispatcher.飞镖:331:7)
E/flutter (11254): #30 _dispatchPointerDataPacket (飞镖:ui/hooks.飞镖:94:31)
E/flutter (11254):
你需要用 jsonEncode() 包裹 body
final body = {
"Parameters": [
{
"Name": "File",
"FileValue": {
"Name": docFilename,
"Data": docFiledata
}
},
{
"Name": "StoreFile",
"Value": true
}
]
};
http.Response response = await http.post(
uri,
body: jsonEncode(body),
headers: {"Content-Type": "application/json"}
);
import 'dart:io';
import 'package:aconvert/pages/view.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'dart:convert';
import 'dart:typed_data';
import 'package:aconvert/services/api.dart';
import 'package:path_provider/path_provider.dart';
import 'package:http/http.dart' as http;
import 'package:pdf_viewer_plugin/pdf_viewer_plugin.dart';
//final clientId = '2720faeb-aa0f-46ee-aa0a-8bf7559268fc';
//final secret = '65cc9d8e63d2a01be5dd5abde8cbc970';
//var configuration = Configuration(secret, clientId);
class Docx extends StatefulWidget {
const Docx({Key? key}) : super(key: key);
@override
_DocxState createState() => _DocxState();
}
class _DocxState extends State<Docx> {
//view pdf
String? docFilePath;
String? docFilename;
String? docFiledata;
File? _file;
late final pathController = TextEditingController();
pick_file() async{
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['doc','txt','TXT','png','jpg','pdf','PDF','rtf'],
);
if (result != null) {
// List<File> files = result.paths.map((path) => File(path!)).toList();
File file = File(result.files.single.path!);
_file = File(result.files.single.path!);
setState(() {
docFilePath =result.files.single.path!;
docFilename = _file?.path.split('/').last;
Uint8List? fileInByte = _file?.readAsBytesSync();
docFiledata = base64Encode(fileInByte!);
print("******** $docFilename **********");
});
} else {
// User canceled the picker
}
}
// convert to pdf api
Future postfile() async{
String url = 'https://v2.convertapi.com/convert/doc/to/pdf?Secret=5Wq3HMYGVzqViAXJ&Token=636106935&StoreFile=true';
final Uri uri = Uri.parse(url);
http.Response response = await http.post(uri,body: {
"Parameters": [
{
"Name": "File",
"FileValue": {
"Name": docFilename,
"Data": docFiledata
}
},
{
"Name": "StoreFile",
"Value": true
}
]
} , headers: {
"Content-Type": "application/json"
});
var responsebody = json.decode(response.body) ;
print('Response status: ${response.statusCode}');
print('------------------------------------');
print('Response body: ${response.body}');
print('------------------------------------');
print('Response body: ${responsebody}');
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('from docx to pdf'),
titleTextStyle: TextStyle(color: Colors.white70 , fontSize: 25 , fontWeight: FontWeight.bold),
backgroundColor: Colors.deepOrangeAccent,
elevation: 0,
centerTitle: true,
),
body: SingleChildScrollView(
child:Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(height: 20,),
InkWell(
onTap: pick_file,
child: Container(
height: 60,
padding: EdgeInsets.all(10),
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.redAccent,
),
child: Center(child: Text('select file ' ,style: TextStyle(fontSize: 30,color: Colors.white), ))),
),
SizedBox(height: 20,),
InkWell(
onTap: (){
Get.to(()=>View(docFilePath!));
},
child: Container(
height: 60,
padding: EdgeInsets.all(10),
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.redAccent,
),
child: Center(child: Text('show selected file' ,style: TextStyle(fontSize: 30,color: Colors.white), ))),
),
SizedBox(height: 20,),
//convert
InkWell(
onTap: postfile,
child: Container(
height: 60,
padding: EdgeInsets.all(10),
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.redAccent,
),
child: Center(child: Text('convert' ,style: TextStyle(fontSize: 30,color: Colors.white), ))),
),
SizedBox(height: 20,),
InkWell(
onTap: (){},
child: Container(
height: 60,
padding: EdgeInsets.all(10),
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.redAccent,
),
child: Center(child: Text('upload to cloud' ,style: TextStyle(fontSize: 30,color: Colors.white), ))),
),
],
),
),
);
}
}
E/flutter (11254): [错误:flutter/lib/ui/ui_dart_state.cc(209)] 未处理的异常:错误状态:无法设置内容类型为“[=19”的请求的正文字段=]”。 E/flutter (11254): #0 Request.bodyFields= (包:http/src/request.dart:133:7) E/flutter (11254): #1 BaseClient._sendUnstreamed (包:http/src/base_client.dart:87:17) E/flutter (11254): #2 BaseClient.post (包:http/src/base_client.dart:32:7) E/flutter (11254): #3 post。 (包:http/http.dart:69:16) E/flutter (11254): #4 _withClient (包:http/http.dart:164:20) E/flutter (11254): #5 post (包:http/http.dart:68:5) E/flutter (11254): #6 _DocxState.postfile (包:aconvert/pages/fromdocxtopdf.dart:61:40) E/flutter (11254): #7 _InkResponseState._handleTap (包:flutter/src/material/ink_well.dart:989:21) E/flutter (11254): #8 GestureRecognizer.invokeCallback (包:flutter/src/gestures/recognizer.dart:198:24) E/flutter (11254): #9 TapGestureRecognizer.handleTapUp (包:flutter/src/gestures/tap.dart:608:11) E/flutter (11254): #10 BaseTapGestureRecognizer._checkUp (包:flutter/src/gestures/tap.dart:296:5) E/flutter (11254): #11 BaseTapGestureRecognizer.handlePrimaryPointer (包:flutter/src/gestures/tap.dart:230:7) E/flutter (11254): #12 PrimaryPointerGestureRecognizer.handleEvent (包:flutter/src/gestures/recognizer.dart:563:9) E/flutter (11254): #13 PointerRouter._dispatch (包:flutter/src/gestures/pointer_router.dart:94:12) E/flutter (11254): #14 PointerRouter._dispatchEventToRoutes。 (包:flutter/src/gestures/pointer_router.dart:139:9) E/flutter (11254): #15 _LinkedHashMapMixin.forEach (飞镖:collection-patch/compact_hash.飞镖:539:8) E/flutter (11254): #16 PointerRouter._dispatchEventToRoutes (包:flutter/src/gestures/pointer_router.dart:137:18) E/flutter (11254): #17 PointerRouter.route (包:flutter/src/gestures/pointer_router.dart:123:7) E/flutter (11254): #18 GestureBinding.handleEvent (包:flutter/src/gestures/binding.dart:439:19) E/flutter (11254): #19 GestureBinding.dispatchEvent (包:flutter/src/gestures/binding.dart:419:22) E/flutter (11254): #20 RendererBinding.dispatchEvent (包:flutter/src/rendering/binding.dart:322:11) E/flutter (11254): #21 GestureBinding._handlePointerEventImmediately (包:flutter/src/gestures/binding.dart:374:7) E/flutter (11254): #22 GestureBinding.handlePointerEvent (包:flutter/src/gestures/binding.dart:338:5) E/flutter (11254): #23 GestureBinding._flushPointerEventQueue (包:flutter/src/gestures/binding.dart:296:7) E/flutter (11254): #24 GestureBinding._handlePointerDataPacket (包:flutter/src/gestures/binding.dart:279:7) E/flutter (11254): #25 _rootRunUnary (飞镖:async/zone.dart:1442:13) E/flutter (11254): #26 _CustomZone.runUnary (飞镖:async/zone.飞镖:1335:19) E/flutter (11254): #27 _CustomZone.runUnary 守卫 (dart:async/zone.dart:1244:7) E/flutter (11254): #28 _invoke1 (飞镖:ui/hooks.飞镖:170:10) E/flutter (11254): #29 PlatformDispatcher._dispatchPointerDataPacket (飞镖:ui/platform_dispatcher.飞镖:331:7) E/flutter (11254): #30 _dispatchPointerDataPacket (飞镖:ui/hooks.飞镖:94:31) E/flutter (11254):
你需要用 jsonEncode() 包裹 body
final body = {
"Parameters": [
{
"Name": "File",
"FileValue": {
"Name": docFilename,
"Data": docFiledata
}
},
{
"Name": "StoreFile",
"Value": true
}
]
};
http.Response response = await http.post(
uri,
body: jsonEncode(body),
headers: {"Content-Type": "application/json"}
);