1

When trying to send form-data that includes an image to the node multer library in the backend, the iOS simulators are giving me the error below in newer ios versions (version 13.7 didn't have any issue sending the image):

Exception '*** -[__NSCFConstantString substringToIndex:]: Index 3 out of bounds; string length 0' was thrown while invoking sendRequest on target Networking with params (
        {
        data =         {
            formData =             (
                                {
                    fieldName = name;
                    headers =                     {
                        "content-disposition" = "form-data; name=\"name\"";
                    };
                    string = "User";
                },
                                {
                    fieldName = email;
                    headers =                     {
                        "content-disposition" = "form-data; name=\"email\"";
                    };
                    string = "[email protected]";
                },
                                {
                    fieldName = topic;
                    headers =                     {
                        "content-disposition" = "form-data; name=\"topic\"";
                    };
                    string = "New Calc Request";
                },
                                {
                    fieldName = comment;
                    headers =                     {
                        "content-disposition" = "form-data; name=\"comment\"";
                    };
                    string = Test;
                },
                                {
                    fieldName = attachment;
                    headers =                     {
                        "content-disposition" = "form-data; name=\"attachment\"; filename=\"\"";
                        "content-type" = "";
                    };
                    name = "";
                    type = "";
                    uri = "";
                }
            );
            trackingName = unknown;
        };

The issue seems to stem from not using JSON.stringify on the attachment. However, when I do use it, I can't seem to parse the image attachment on the server for use by the multer library. (Note: the attachment info is empty in the error because the simulator doesn't work well with the M1 chip in ios verisons later than 13).

Form-data being created as such:

userFeedBackInputs.append('name', this.currentUser.givenName + ' ' + this.currentUser.surname);
userFeedBackInputs.append('email', this.currentUser.email);
userFeedBackInputs.append('topic', this.state.topic.value);
userFeedBackInputs.append('comment', (this.state.comment.value : ''));
userFeedBackInputs.append('attachment', {
          uri: this.state.imageSrc.value,
          name: this.state.imageSrc.fileName,
          type: this.state.imageSrc.type
       }
 )

How can I either a) send the data without using JSON.stringify or b) parse the JSON of the attachment within the formdata BEFORE it gets to the multer library. (It seems like the usual node body-parsers don't work with nested JSON, since this is a multi-form request)

1 Answer 1

1

I have exactly the same issue when test my app with ios devices that run ios 13, and I found the solution.

The reason is because the attachment has no data, just log the 'this.state.imageSrc.value' 'this.state.imageSrc.fileName' 'this.state.imageSrc.type', you'll see imageSrc is empty, may be there is a bug related to the file picker library,

if you're using 'react-native-image-crop-picker', update it to 0.33.3 then delete Node_modules, Pods, and re run, problem'll be solved

more detail : https://github.com/ivpusic/react-native-image-crop-picker/issues/1130

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.