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)