Browse Source

Change native content type checking logic

Ben Hsieh 8 years ago
parent
commit
04b7785a86

+ 1
- 1
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobBody.java View File

94
             String header = "--" + boundary + "\r\n";
94
             String header = "--" + boundary + "\r\n";
95
             if (field.filename != null) {
95
             if (field.filename != null) {
96
                 header += "Content-Disposition: form-data; name=" + name + "; filename=" + field.filename + "\r\n";
96
                 header += "Content-Disposition: form-data; name=" + name + "; filename=" + field.filename + "\r\n";
97
-                header += "Content-Type: " + field.mime+ "\r\n\r\n";
97
+                header += "Content-Type: " + field.mime + "\r\n\r\n";
98
                 sink.write(header.getBytes());
98
                 sink.write(header.getBytes());
99
                 // file field header end
99
                 // file field header end
100
                 // upload from storage
100
                 // upload from storage

+ 5
- 0
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

213
                         requestType = RequestType.SingleFile;
213
                         requestType = RequestType.SingleFile;
214
                     }
214
                     }
215
                     else if (cType.toLowerCase().contains(";base64") || cType.toLowerCase().startsWith("application/octet")) {
215
                     else if (cType.toLowerCase().contains(";base64") || cType.toLowerCase().startsWith("application/octet")) {
216
+                        cType = cType.replace(";base64","").replace(";BASE64","");
217
+                        if(mheaders.containsKey("content-type"))
218
+                            mheaders.put("content-type", cType);
219
+                        if(mheaders.containsKey("Content-Type"))
220
+                            mheaders.put("Content-Type", cType);
216
                         requestType = RequestType.SingleFile;
221
                         requestType = RequestType.SingleFile;
217
                     } else {
222
                     } else {
218
                         requestType = RequestType.AsIs;
223
                         requestType = RequestType.AsIs;

+ 5
- 0
src/ios/RNFetchBlobReqBuilder.m View File

119
                     // when content-type is application/octet* decode body string using BASE64 decoder
119
                     // when content-type is application/octet* decode body string using BASE64 decoder
120
                     if([[cType lowercaseString] hasPrefix:@"application/octet"] || [[cType lowercaseString] containsString:@";base64"])
120
                     if([[cType lowercaseString] hasPrefix:@"application/octet"] || [[cType lowercaseString] containsString:@";base64"])
121
                     {
121
                     {
122
+                        __block NSString * ncType = [[cType stringByReplacingOccurrencesOfString:@";base64" withString:@""]stringByReplacingOccurrencesOfString:@";BASE64" withString:@""];
123
+                        if([mheaders valueForKey:@"content-type"] != nil)
124
+                            [mheaders setValue:ncType forKey:@"content-type"];
125
+                        if([mheaders valueForKey:@"Content-Type"] != nil)
126
+                            [mheaders setValue:ncType forKey:@"Content-Type"];
122
                         blobData = [[NSData alloc] initWithBase64EncodedString:body options:0];
127
                         blobData = [[NSData alloc] initWithBase64EncodedString:body options:0];
123
                         [request setHTTPBody:blobData];
128
                         [request setHTTPBody:blobData];
124
                         size = [blobData length];
129
                         size = [blobData length];

+ 4
- 0
src/polyfill/Fetch.js View File

47
         // When request body is a Blob, use file URI of the Blob as request body.
47
         // When request body is a Blob, use file URI of the Blob as request body.
48
         else if (body.isRNFetchBlobPolyfill)
48
         else if (body.isRNFetchBlobPolyfill)
49
           promise = Promise.resolve(RNFetchBlob.wrap(body.blobPath))
49
           promise = Promise.resolve(RNFetchBlob.wrap(body.blobPath))
50
+        else if (typeof body !== 'object')
51
+          promise = Promise.resolve(JSON.stringify(body))
52
+        else if (typeof body !== 'string')
53
+          promise = Promise.resolve(body.toString())
50
         // send it as-is, leave the native module decide how to send the body.
54
         // send it as-is, leave the native module decide how to send the body.
51
         else
55
         else
52
           promise = Promise.resolve(body)
56
           promise = Promise.resolve(body)