0

I cannot send my base64 String with the Post Function, and the only error I get is: "Bad state: Cannot set the body fields of a Request with content-type 'multipart/form-data'." when I remove the header, I get this error: "XMLHttpRequest error." Does anyone know how I can do that? I also tried base64 code from an Online Converter to be sure my String is ok.

TextButton(
            onPressed: () async {
              final base64 = await documentToBase64(document);
              final urlEncBase64 = Uri.encodeComponent(base64);
              final uri = Uri.parse(myurl);
              await http.post(uri,
                headers: {
                  'content-type': 'multipart/form-data'
                },
                body: {
                  'b64': '$urlEncBase64',
                }).then((res) {
                  print(res.statusCode);
                }).catchError((err) {
                  print(err);
                }
              );
            },
            child: const Text('Send'),
          ),
Frank92
  • 1
  • 2

2 Answers2

0

If u want to send data as String in JSON format u should use another content-type

'content-type': 'application/json'

But if u want to use multipart/form-data type u should stick to this guide. https://dev.to/carminezacc/advanced-flutter-networking-part-1-uploading-a-file-to-a-rest-api-from-flutter-using-a-multi-part-form-data-post-request-2ekm

Nazar Kokhan
  • 87
  • 1
  • 3
0

The correct Answer was to disable web security because i tried to Access to a PHP trough a remote Connection. How to solve flutter web api cors error only with dart code?

And delete the wrong header.

Frank92
  • 1
  • 2