0

I'm trying to send a string from a java (android) app to a node.js server. But one character disappears somewhere in the middle and I can't really figure out why.

To send I use a HttpUrlConnection (conn) and send the string like this:

try {
    OutputStream os = conn.getOutputStream();
    os.write(json.getBytes());
    os.close();
} catch (Exception e) {
    e.printStackTrace();
}

Here is the base64 encoded string when sent, and string when received:

khVGUBH2kNAR5PPRy7v5dO5iz48Rc7benYARu78\/9wY=\n
khVGUBH2kNAR5PPRy7v5dO5iz48Rc7benYARu78/9wY=\n

so one backslash has be removed.

In node I use this:

exports.getString = function(req, res) {
    var string = req.body.thestring;
}

which outputs the later of the two strings.

var express = require('express'),
    http = require('http'),
    stylus = require('stylus'),
    nib = require('nib');

var app = express();

app.configure(function () {
    app.use(express.logger('dev'));
    //app.use(express.bodyParser());
    app.use(express.json());
    app.use(express.urlencoded());
    app.use(app.router);
}

Any ideas of how I can get the missing character?

just_user
  • 11,769
  • 19
  • 90
  • 135
  • The Backslash is not important for decoding Base64. I suppose your application is not suffering from the missing character. Or do you have real problems with this? – hgoebl Dec 14 '13 at 17:53
  • Offtopic: If you don't want to waste your time fighting with `HttpUrlConnection` you could consider using [DavidWebb](https://github.com/hgoebl/DavidWebb) or another HTTP-/REST library listed there. – hgoebl Dec 14 '13 at 17:56
  • @hgoebl Thanks, it is kind of important because this string is a part of a bigger things that I make a hash of on the server side to confirm the integrity of the package received. And if that backslash is missing I have two different hashes in the end. Thanks for the library recommendation. I've tried it by my body is always empty. If you can find an answer to my other question:http://stackoverflow.com/questions/20543115/httppost-setentity-always-empty I'd be grateful! – just_user Dec 14 '13 at 18:02
  • I answered it and with my example code the body is not empty. If you still encounter problems, you should give us some insight on your node.js code. – hgoebl Dec 14 '13 at 21:38
  • @hgoebl great library, thanks! Still the backslash isn't showing in node but changing the base64 encoding to url safe did it. – just_user Dec 15 '13 at 11:01

2 Answers2

0

The missing backslash character is most probably disappearing in node.js side.

As per the chosen answer on the following question: Two part question on JavaScript forward slash

As far as JS is concerned / and \ / are identical inside a string

So maybe a fix from Java's would solve your problem by using String's replaceAll method to replace all occurrences of \/ with \\/:

os.write(json.replaceAll("\\/", "\\\\/").getBytes());

Note that replaceAll returns the new string and doesn't change the original string.

Community
  • 1
  • 1
javatar
  • 41
  • 1
0

Making the base64 encoding url safe solved my problem.

just_user
  • 11,769
  • 19
  • 90
  • 135