Unrecognized FormData part

#1

I’m getting that when i add a pdf to formData (in detached expo):

var fd = new FormData();
fd.append('filename', {
              uri: this.state.pdfPath,
              type: 'application/pdf'
            });

Tried with valid content:// and file:// paths.
without the file and only normal string values, it send the data.

commented this because it can’t work with it:

    // global.XMLHttpRequest = global.originalXMLHttpRequest ?
    //   global.originalXMLHttpRequest :
    //   global.XMLHttpRequest;
    // global.FormData = global.originalFormData ?
    //   global.originalFormData :
    //   global.FormData;

The error is coming from
node_modules\react-native\ReactAndroid\src\main\java\com\facebook\react\modules\network\NetworkingModule.java
in this code:

private @Nullable MultipartBody.Builder constructMultipartBody(
      ReadableArray body,
      String contentType,
      int requestId) {
    RCTDeviceEventEmitter eventEmitter = getEventEmitter();
    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
    multipartBuilder.setType(MediaType.parse(contentType));

    Log.i("--------------MULTIPART", contentType);
    Log.i("--------------MULTIPART", requestId);

    for (int i = 0, size = body.size(); i < size; i++) {
      ReadableMap bodyPart = body.getMap(i);

      Log.i("--------------MULTIPART i", i);
      Log.i("--------------MULTIPART str", bodyPart.toString());
    
      // Determine part's content type.
      ReadableArray headersArray = bodyPart.getArray("headers");
      Headers headers = extractHeaders(headersArray, null);
      if (headers == null) {
        ResponseUtil.onRequestError(
          eventEmitter,
          requestId,
          "Missing or invalid header format for FormData part.",
          null);
        return null;
      }
      MediaType partContentType = null;
      String partContentTypeStr = headers.get(CONTENT_TYPE_HEADER_NAME);
      if (partContentTypeStr != null) {
        partContentType = MediaType.parse(partContentTypeStr);
        // Remove the content-type header because MultipartBuilder gets it explicitly as an
        // argument and doesn't expect it in the headers array.
        headers = headers.newBuilder().removeAll(CONTENT_TYPE_HEADER_NAME).build();
      }

      Log.i("--------------MULTIPART REQUEST_BODY_KEY_STRING", REQUEST_BODY_KEY_STRING);
      Log.i("--------------MULTIPART REQUEST_BODY_KEY_URI", REQUEST_BODY_KEY_URI);

      if (bodyPart.hasKey(REQUEST_BODY_KEY_STRING)) {
        String bodyValue = bodyPart.getString(REQUEST_BODY_KEY_STRING);
        multipartBuilder.addPart(headers, RequestBody.create(partContentType, bodyValue));
      } else if (bodyPart.hasKey(REQUEST_BODY_KEY_URI)) {
        if (partContentType == null) {
          ResponseUtil.onRequestError(
            eventEmitter,
            requestId,
            "Binary FormData part needs a content-type header.",
            null);
          return null;
        }
        String fileContentUriStr = bodyPart.getString(REQUEST_BODY_KEY_URI);
        InputStream fileInputStream =
            RequestBodyUtil.getFileInputStream(getReactApplicationContext(), fileContentUriStr);
        if (fileInputStream == null) {
          ResponseUtil.onRequestError(
            eventEmitter,
            requestId,
            "Could not retrieve file for uri " + fileContentUriStr,
            null);
          return null;
        }
        multipartBuilder.addPart(headers, RequestBodyUtil.create(partContentType, fileInputStream));
      } else {
        ResponseUtil.onRequestError(eventEmitter, requestId, "Unrecognized FormData part.", null);
      }
    }
    return multipartBuilder;
  }

you can see i added some Log.i() but i can’t see the logs in android studio monitor for some reason…

#2

I’m getting exactly the same error just on Android… Any fix for now ?