Uploading Image to Node.js express server Swift - node.js

I am currently creating an app in which I will send an image from an iOS device to my expressjs server, using the multer middleware. I have the server side set up, yet I believe I have an error on the client side, because I cannot get my POST request to work correctly. Here is my swift code.
class func changeChannelImage(handle: String, imageURL: URL, completionHandler: #escaping (Int?, Error?) -> Void){
let baseURL = "http://10.0.0.220:3000/channel/channelImage?handle=\(handle)"
func createRequestBodyWith(parameters:[String:NSObject], boundary:String) -> Data{
var body = Data()
for (key, value) in parameters {
body.appendString(string: "--\(boundary)\r\n")
body.appendString(string: "Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString(string: "\(value)\r\n")
}
body.appendString(string: "--\(boundary)\r\n")
var mimetype = "image/png"
let defFileName = "channelImage.png"
print(imageURL.absoluteString)
do {
var data = try Data(contentsOf: imageURL) //Image file URL in device's directory
var image = UIImage(data: data)
let imageData = UIImagePNGRepresentation(image!)
body.appendString(string: "Content-Disposition: form-data; filename=\"\(defFileName)\"\r\n")
print("Content-Type: \(mimetype)\r\n\r\n")
body.appendString(string: "Content-Type: \(mimetype)\r\n\r\n")
body.append(imageData!)
body.appendString(string: "\r\n")
body.appendString(string: "--\(boundary)--\r\n")
}
catch {
print(error.localizedDescription)
}
return body
}
func generateBoundaryString() -> String {
return "Boundary-\(NSUUID().uuidString)"
}
var request = URLRequest(url: URL(string: baseURL)!)
request.httpMethod = "POST"
let boundary = generateBoundaryString()
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.httpBody = createRequestBodyWith(parameters: [:], boundary: generateBoundaryString())
let session = URLSession.shared
let task = session.dataTask(with: request) { (data, response, error) in
if error == nil {
let res = response as! HTTPURLResponse
let code = res.statusCode
if code != 200 {
print(String(data: data!, encoding: .utf8))
}
DispatchQueue.main.async {
completionHandler(code, nil)
}
}
else {
print(error!)
DispatchQueue.main.async {
completionHandler(0, error)
}
}
}
task.resume()
}
Does anyone have any idea why the image file cannot be read properly by the server? Any help would be appreciated.

Convert your image data into base64 String and then append it into body

Related

Getting 413 Error when calling Express Api from Swift

When I try to access my Express endpoint that I made in node from my iOS app, I get a 413 error, saying that the payload is too large. Here is my Node code.
router.post('/images/tags/nutritionalInformation/image/base64encoded', function (req, res) {
var base64Stream = req.body.imageString;
var imgDecodedBuffer = decodeBase64Image(base64Stream);
// write to image file
var prefix = guid().toString() + ".jpg";
var filePath = './uploads/' + prefix;
return s3fsImpl.writeFile(prefix, imgDecodedBuffer.data).then(function () {
var s3BaseUrl = "https://s3.amazonaws.com/app/" + prefix;
console.log(s3BaseUrl);
app.models.predict('KEY', s3BaseUrl).then(
function (response) {
var responseJson = JSON.stringify(response.data.outputs[0].data.concepts);
var data = collectTags(responseJson);
data.then(function (value) {
res.json(value);
});
},
function (err) {
console.log(err);
});
});
})
I tested this out in postman and when I attach the base64 encoded string in the body, it works fine! However, when I try to send a POST call from Swift using the following Code :
func getTags() {
let image : UIImage = UIImage(named: "burger.jpg")!
let imageData = UIImageJPEGRepresentation(image, 1.0)
// base64 encode imagedata
let prefix = "data:image/jpeg;base64,"
let strBase64:String = prefix + (imageData?.base64EncodedString(options: .lineLength64Characters))!
// Call tagging api https://foodenodeapp.herokuapp.com/api/images/tags/nutritionalInformation/image/base64encoded
var request = URLRequest(url: URL(string: "https://foodenodeapp.herokuapp.com/api/images/tags/nutritionalInformation/image/base64encoded")!)
request.httpMethod = "POST"
let poststring = "imageString=" + strBase64
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
print(data)
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString)")
}
task.resume()
}
However, when I print out the responseString in the Swift Code, I get this response:
nutritionalInformation/image/base64encoded } { status code: 413, headers {
Connection = "keep-alive";
"Content-Length" = 18;
"Content-Type" = "text/html; charset=utf-8";
Date = "Sun, 04 Dec 2016 20:18:15 GMT";
Server = Cowboy;
Via = "1.1 vegur";
"X-Content-Type-Options" = nosniff;
"X-Powered-By" = Express;
} }
18 bytes
responseString = Optional("Payload Too Large\n")
So I did some research and I tried changing my app.js, which is my root file (the logic code is in api.js).
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({extended:true, limit: '50mb'}));
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
res.redirect('/api');
});
app.use('/api', require('./routes/api'));
However, when after this change, when I run the function in Swift, I now get a 500 error.
Any help resolving this issue would be greatly appreciated!
This problem occurs from the server side.
If your server is Node JS then make changes as:-
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
app.use(bodyParser.json({ limit: '10mb' })) // change limit according to you.
for other server change limit.

org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request...Stream ended unexpectedly

SITUATION:
Submitting a multipart form request from Node.js (via node core HTTPS module) to a spring-boot Java API. The API requires two form-data elements:
"route"
"files"
FULL ERROR:
Exception processed - Main Exception:
org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: Stream ended unexpectedly
REQUEST HEADERS:
{"Accept":"*/*",
"cache-control":"no-cache",
"Content-Type":"multipart/form-data; boundary=2baac014-7974-49dd-ae87-7ce56c36c9e7",
"Content-Length":7621}
FORM-DATA BEING WRITTEN (all written as binary):
Content-Type: multipart/form-data; boundary=2baac014-7974-49dd-ae87-7ce56c36c9e7
--2baac014-7974-49dd-ae87-7ce56c36c9e7
Content-Disposition:form-data; name="route"
...our route object
--2baac014-7974-49dd-ae87-7ce56c36c9e7
Content-Disposition:form-data; name="files"; filename="somefile.xlsx"
Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
...excel file contents
--2baac014-7974-49dd-ae87-7ce56c36c9e7--
NODE CODE:
let mdtHttpMultipart = (options, data = reqParam('data'), cb) => {
const boundaryUuid = getUuid()
, baseHeaders = {
'Accept': '*/*',
'cache-control': 'no-cache'
}
, composedHeaders = Object.assign({}, baseHeaders, options.headers)
;
options.path = checkPath(options.path);
let composedOptions = Object.assign({}, {
'host': getEdiHost(),
'path': buildPathFromObject(options.path, options.urlParams),
'method': options.method || 'GET',
'headers': composedHeaders,
'rejectUnauthorized': false
});
composedOptions.headers['Content-Type'] = `multipart/form-data; boundary=${boundaryUuid}`;
let multipartChunks = [];
let dumbTotal = 0;
let writePart = (_, encType = 'binary', skip = false) => {
if (!_) { return; }
let buf = Buffer.from(_, encType);
if (!skip) {dumbTotal += Buffer.byteLength(buf, encType);}
multipartChunks.push(buf);
};
writePart(`Content-Type: multipart/form-data; boundary=${boundaryUuid}\r\n\r\n`, 'binary', true)
writePart(`--${boundaryUuid}\r\n`)
writePart(`Content-Disposition:form-data; name="route"\r\n`)
writePart(JSON.stringify(data[0]) + '\r\n')
writePart(`--${boundaryUuid}\r\n`)
writePart(`Content-Disposition:form-data; name="files"; filename="${data[1].name}"\r\n`)
writePart(`Content-Type:${data[1].contentType}\r\n`)
writePart(data[1].contents + '\r\n')
writePart(`\r\n--${boundaryUuid}--\r\n`);
let multipartBuffer = Buffer.concat(multipartChunks);
composedOptions.headers['Content-Length'] = dumbTotal;
let request = https.request(composedOptions);
// on nextTick write multipart to request
process.nextTick(() => {
request.write(multipartBuffer, 'binary');
request.end();
});
// handle response
request.on('response', (httpRequestResponse) => {
let chunks = []
, errObject = handleHttpStatusCodes(httpRequestResponse);
;
if (errObject !== null) {
return cb(errObject, null);
}
httpRequestResponse.on('data', (chunk) => { chunks.push(chunk); });
httpRequestResponse.on('end', () => {
let responseString = Buffer.concat(chunks).toString()
;
return cb(null, JSON.parse(responseString));
});
});
request.on('error', (err) => cb(err));
};
We cannot see any reason for the 500 to be thrown based on the spec. Tons of tinkering around with the format here but we have yet to achieve the result correctly.
SIDE NOTE: It works for us using POSTMAN, just can't get it to work using our our own application server (where we actually build the excel file).
Any help would be appreciated even if just ideas to try.
Try this:
let mdtHttpMultipart = (options, data = reqParam('data'), cb) => {
const boundaryUuid = getUuid()
, baseHeaders = {
'Accept': '*/*',
'cache-control': 'no-cache'
}
, composedHeaders = Object.assign({}, baseHeaders, options.headers)
;
let file = data[1]
let xlsx = file.contents
options.path = checkPath(options.path);
let composedOptions = Object.assign({}, {
'host': getEdiHost(),
'path': buildPathFromObject(options.path, options.urlParams),
'method': options.method || 'GET',
'headers': composedHeaders,
'rejectUnauthorized': false
});
let header = Buffer.from(`--${boundaryUuid}
Content-Disposition: form-data; name="route"
${JSON.stringify(data[0])})
--${boundaryUuid}
Content-Disposition: form-data; name="files"; filename="${file.name}"
Content-Type: ${file.contentType}
`.replace(/\r?\n */gm, '\r\n'))
let footer = Buffer.from(`\r\n--${boundaryUuid}--`)
let length = header.length + xlsx.length + footer.length
let body = Buffer.concat([header, xlsx, footer], length)
composedOptions.headers['Content-Length'] = length;
composedOptions.headers['Content-Type'] = `multipart/form-data; boundary=${boundaryUuid}`;
let request = https.request(composedOptions);
// handle response
request.on('response', (httpRequestResponse) => {
let chunks = []
, errObject = handleHttpStatusCodes(httpRequestResponse);
;
if (errObject !== null) {
return cb(errObject, null);
}
httpRequestResponse.on('data', (chunk) => { chunks.push(chunk); });
httpRequestResponse.on('end', () => {
let responseString = Buffer.concat(chunks).toString()
;
return cb(null, JSON.parse(responseString));
});
});
request.on('error', (err) => cb(err));
// write multipart to request
request.end(body);
};
Is it that you're not calling request.end() anywhere?
The (very general) form for sending a request with a body is https.request(opts).end(body).
Also, you could just call request.write(buf) every time you want to send data instead of accumulating into one giant buffer (and you shouldn't need to do it on nextTick) (EDIT: as OP points out in the comments, this will prevent setting Content-Length, so perhaps keep as is)

Groovy : Cannot set a request body for a DELETE/GET method

When i am executing this via curl its working :
curl -u -X DELETE -H 'accept:application/json' http://localhost:13000/test/test_userid"
I made a common function which accept methodtype ( GET, POST, DELETE etc) and content type( JASON, TEXT ) for the httpbuilder.
def public httpRequest(String url, String content, Method requestType, ContentType contentType)
{
try{
def myClient = new HTTPBuilder(url)
myClient.request(requestType,contentType) { req ->
headers.'Content-Type' = 'application/json'
body=content
response.success = { resp, data ->
def reponse=[resp:resp,data:data]
return reponse
}
response.failure = { resp ->
println 'Response Code '+resp.statusLine
}
// called only for a 404 (not found) status code:
response.'404' = { resp ->
println 'Not found'
}
}
}
catch(Exception e)
{
println "error"+e.getProperties()
}
}
Now if i make a POST request , its working.
However if i make a GET or DELETE request using
def response = httpRequest(url,"",DELETE,JSON)
or
def response = httpRequest(url,"",GET,TEXT)
its shows the following error :-
error[message:Cannot set a request body for a DELETE/GET method, class:class java.lang.IllegalArgumentException
Do i need to make a separate function for GET/DELETE?
because
myClient.request(requestType) { req ->
headers.'Content-Type' = 'application/json'
body=content
response.success = { resp, data ->
def reponse=[resp:resp,data:data]
return reponse
}
response.failure = { resp ->
println 'Response Code '+resp.statusLine
}
// called only for a 404 (not found) status code:
response.'404' = { resp ->
println 'Not found'
}
}
}
WORKS
Delete and Get wont accept Body , hence the solution is to make a check and execute accordingly
if(requestType.equals(DELETE)||requestType.equals(GET))
{
try{
def myClient = new HTTPBuilder(url)
myClient.request(requestType) { req ->
headers.'Content-Type' = 'application/json'
headers.Accept = 'application/json'
response.success = { resp, data ->
def reponse=[resp:resp,data:data]
return reponse
}
response.failure = { resp ->
println 'Response Code '+resp.statusLine
}
// called only for a 404 (not found) status code:
response.'404' = { resp ->
println 'Not found'
}
}
}
catch(Exception e)
{
println "error"+e.getProperties()
}
}
else
<---post request -->

Plupload Error -200 HTTP Error

I am trying to move the uploading on a remote server. After I choose a file with the code below and click upload the file IS uploaded, but an error returns saying code: "-200" message: "HTTP Error"
var uploader = new plupload.Uploader(
{
runtimes : 'html4, html5, flash, silverlight',
browse_button : 'bt_browse',
container: document.getElementById('container'),
url : 'http://remote.com/upload.php',
silverlight_xap_url : 'js/Moxie.xap',
chunks_size: '20mb',
max_retries: 3,
filters : {
max_file_size : '100mb'
},
multi_selection : true,
init: {
PostInit: function() {
document.getElementById('filelist').innerHTML = '';
document.getElementById('bt_uploadfiles').onclick = function() {
uploader.start();
return false;
};
},
FilesAdded: function(up, files) {
plupload.each(files, function(file) {
//build list
}},
UploadProgress: function(up, file) {
$("#progressBar"+file.id).val(Math.round(file.percent));
if(Math.round(file.percent)==100){
$("#progressBar"+file.id).hide();
$("#deleteFile" + file.id).hide();
}
},
FileUploaded: function(up, file, info) {
if(file!=undefined) {
var json = $.parseJSON(info.response);
if(json.error == undefined)
moveFile(json.result, file.name, file.id);
}
},
UploadComplete: function(){
},
Error: function(up, err) {
}
}
});
What can I do to escape this error and continue? In my case FileUploaded and UploadProgress are not hit at all - after I hit upload I directly moved to Error function. This is really weird for me since after that I check the folder where it is supposed to be and the file is there.
Any help will be much appreciated.
I came across the same error when I was using PlUpload in an MVC5 application. The problem was that the REST method could not be found. PlUpload using a multipart for data. The code below shows how this could be implemented in a WebAPI
public class UploadFilesController : ApiController
{
[HttpPost]
[Route("UploadFiles")]
public async Task<HttpResponseMessage> PostFormData()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
try
{
// Read the form data.
await Request.Content.ReadAsMultipartAsync(provider);
var TestId = provider.FormData.Get("TestId");
var chunk = provider.FormData.Get("chunk");
var chunks = provider.FormData.Get("chunks");
var fileName = provider.FormData.Get("name");
int chunkId = Convert.ToInt32(chunk);
int totalChunks = Convert.ToInt32(chunks);
Boolean isLastChunch = chunkId == totalChunks - 1;
foreach (MultipartFileData file in provider.FileData)
{
//Console.WriteLine(file.Headers.ContentDisposition.FileName);
//Console.WriteLine("Server file path: " + file.LocalFileName);
string FileDestination = Path.GetDirectoryName(file.LocalFileName) + #"\" + fileName;
using (FileStream fileUpload = new FileStream(file.LocalFileName, FileMode.Open))
{
using (var fs = new FileStream(FileDestination, chunkId == 0 ? FileMode.Create : FileMode.Append))
{
var buffer = new byte[fileUpload.Length];
fileUpload.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, buffer.Length);
}
}
File.Delete(file.LocalFileName);
}
if (isLastChunch) {
// Do something with the completed file
}
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}

Uncompress gzipped http request body to json in Node.js

I have a windows 8 application connecting to a web service written in Node.js. On the windows 8 side I compressed my request body to gzip. But on the Node.js side I found that my req.body type was Object.
I cannot use zlib to uncomporess the body since it's not a stream.
I can use zlib to uncomporess the req, but I don't know how to retrieve the req.body content from the unzipped stream and parse the body in JSON format.
BTW, I reviewed my request through Fiddler and it told me the request body was gzipped, and I can see my raw body through Fiddler after unzipped so the request should be correct.
Updated
Below is my Node.js app
(function () {
var express = require("express");
var zlib = require("zlib");
var app = express();
var port = 12345;
app.configure(function () {
app.use(express.compress());
app.use(express.bodyParser());
});
app.post("/test", function (req, res) {
var request = req.body;
req.pipe(zlib.createGunzip());
var response = {
status: 0,
value: "OK"
};
res.send(200, response);
});
console.log("started at port %d", port);
app.listen(port);
})();
And below is my windows store app code (partial)
private async void button1_Click_1(object sender, RoutedEventArgs e)
{
var message = new
{
Name = "Shaun",
Value = "12345678901234567890123456789012345678901234567890"
};
var json = await JsonConvert.SerializeObjectAsync(message, Formatting.Indented);
var bytes = Encoding.UTF8.GetBytes(json);
var client = new HttpClient();
client.BaseAddress = new Uri("http://192.168.56.1:12345/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.ExpectContinue = false;
var jsonContent = new JsonContent(message);
var gzipContent = new GZipContent3(jsonContent);
var res = await client.PostAsync("test", gzipContent);
var dialog = new Windows.UI.Popups.MessageDialog(":)", "完成");
await dialog.ShowAsync();
}
internal class GZipContent3 : ByteArrayContent
{
public GZipContent3(HttpContent content)
: base(LoadGZipBytes(content))
{
//base.Headers.ContentType = content.Headers.ContentType;
base.Headers.ContentType = new MediaTypeHeaderValue("x-application/x-gzip");
base.Headers.ContentEncoding.Add("gzip");
}
private static byte[] LoadGZipBytes(HttpContent content)
{
var source = content.ReadAsByteArrayAsync().Result;
byte[] buffer;
using (var outStream = new MemoryStream())
{
using (var gzip = new GZipStream(outStream, CompressionMode.Compress, true))
{
gzip.Write(source, 0, source.Length);
}
buffer = outStream.ToArray();
}
return buffer;
}
}
internal class JsonContent : StringContent
{
private const string defaultMediaType = "application/json";
public JsonContent(string json)
: base(json)
{
var mediaTypeHeaderValue = new MediaTypeHeaderValue(defaultMediaType);
mediaTypeHeaderValue.CharSet = Encoding.UTF8.WebName;
base.Headers.ContentType = mediaTypeHeaderValue;
}
public JsonContent(object content)
: this(GetJson(content))
{
}
private static string GetJson(object content)
{
if (content == null)
{
throw new ArgumentNullException("content");
}
var json = JsonConvert.SerializeObject(content, Formatting.Indented);
return json;
}
}
http://www.senchalabs.org/connect/json.html. Basically you need to write your own middleware based on connect.json() that pipes through an uncompression stream like connect.compress() but the opposite direction: http://www.senchalabs.org/connect/compress.html
Also, make sure you're sending the correct Content-Encoding header in your request.
If you show me what you have so far I can help you further.
I was working on similar thing and finally landed on
function getGZipped(req, callback) {
var gunzip = zlib.createGunzip();
req.pipe(gunzip);
var buffer = [];
gunzip.on('data', function (data) {
// decompression chunk ready, add it to the buffer
buffer.push(data);
}).on('end', function () {
//response and decompression complete, join the buffer and return
callback(null, JSON.parse(buffer));
}).on('error', function (e) {
callback(e);
});
}

Resources