Javascript Base64 Decoding to Invalid JSON Output - json

I'm using a Firebase Cloud function to receive a JSON payload in an http request from the App Store (server-to-server notifications) that contains a Base64 encoded string. I'm decoding using:
const latestReceipt = request.body.latest_receipt ? Buffer.from(request.body.latest_receipt, 'base64').toString() : null;
This is works fine in another function to decode a message coming from the Google Play Store. The decoded string coming from Apple, however, is an invalid JSON object.
{
"original-purchase-date-pst" = "2019-09-20 16:40:20 America/Los_Angeles";
"quantity" = "1";
"subscription-group-identifier" = "****";
"unique-vendor-identifier" = "****";
"original-purchase-date-ms" = "1569022820000";
"expires-date-formatted" = "2019-09-24 02:55:47 Etc/GMT";
"is-in-intro-offer-period" = "false";
"purchase-date-ms" = "1569293447000";
"expires-date-formatted-pst" = "2019-09-23 19:55:47 America/Los_Angeles";
"is-trial-period" = "false";
"item-id" = "1478806339";
"unique-identifier" = "******";
"original-transaction-id" = "1000000570864027";
"expires-date" = "1569293747000";
"transaction-id" = "1000000571530192";
"bvrs" = "11";
"web-order-line-item-id" = "1000000047099385";
"version-external-identifier" = "0";
"bid" = "****";
"product-id" = "storage_increase";
"purchase-date" = "2019-09-24 02:50:47 Etc/GMT";
"purchase-date-pst" = "2019-09-23 19:50:47 America/Los_Angeles";
"original-purchase-date" = "2019-09-20 23:40:20 Etc/GMT";
}
I'm assuming that I'm doing something wrong, but I'm not sure what exactly. I wouldn't expect the App Store to send invalid JSON.

Related

Better/less verbose way to map a complex JSON response to a model using Jackson

I am trying to map a complex JSON response to a domain model class using Jackson 2. The domain model is a flattened version of the list of objects returned in the HTTP response. Another complication is that the domain model's field names are different from the ones returned in the JSON response, to complicate things more the JSON response consists of a list of the domain model objects I am trying to map as well as other info related to the response. Other than getting the list of domain objects from the response I also want to get some of the HTTP Response data that is not part of the array of data objects returned I managed to do this by using the readTree function and then indexing into the keys of the JSON tree. this is quite frustrating as I have to check for null values as not all fields are aways returned. Is there a better way to achieve what I did in the code below? I am planning to copy this to a custom mapper class which I will then register with the Object mapper but it feels very verbose/like there should be a better way to do this other than creating DTO's that models the response?
my code:
var export = CxExport()
var response = mapper.readTree(body)
var count = response["response"]["count"].asInt()
export.exportCount = count
export.accountId= 123456
export.exportType = ExportType.mention
export.exportStart = Timestamp.from(Instant.now())
export.exportEnd = Timestamp.from(Instant.now())
cxExportRepo.saveAndFlush(export)
var mentions: MutableList<Mention> = ArrayList()
var data = response["response"]["data"]
data.forEach {
println(it["permalink"]?.asText())
var locContinent: String? = null
var locCountry: String? = null
var locCity: String? = null
var locRegion: String? = null
var locLongitude: String? = null
var locLatitude: String? = null
var priorityId: Int? = null
var priorityName: String? = null
var priorityColor: String? = null
it["priority"]?.let {
priorityId = it["id"]?.asInt()
priorityColor = it["color"]?.asText()
priorityName = it["name"]?.asText()
}
it["location"]?.let {
locContinent = it["continent"]?.asText()
locCountry = it["country"]?.asText()
locCity = it["city"]?.asText()
locRegion = it["region"]?.asText()
locLongitude = it["longitude"]?.asText()
locLatitude = it["latitude"]?.asText()
}
var tags: String? = null
it["author"]["tags"]?.forEach {
it?.asText()?.let {
tags += it
}
}
mentions.add(Mention(
cxExport = export,
authorId = it["author"]["id"]?.asText(),
authorUrl = it["author"]["url"]?.asText(),
authorImg = it["author"]["img"]?.asText(),
authorTags = tags,
messageTitle =it["message"]["title"]?.asText(),
messageContent =it["message"]["content"]?.asText(),
messageLanguage = it["message"]["language"]?.asText(),
messageSentiment =it["message"]["sentiment"]?.asText(),
sourceId = it["source"]["id"]?.asText(),
sourceCategory = it["source"]["category"]?.asText(),
sourceType = it["source"]["type"]?.asText(),
sourceDomain = it["source"]["domain"]?.asText(),
sourceUrl = it["source"]["url"]?.asText(),
sourceProfile = it["source"]["profile_id"]?.asText(),
sourceProfileName = it["source"]["profile_name"]?.asText(),
locContinent = locContinent,
locCountry = locCountry,
locCity = locCity,
locRegion = locRegion,
locLongitude = locLongitude,
locLatitude = locLatitude,
permalink = it["permalink"]?.asText(),
priorityId = priorityId,
priorityName = priorityName,
priorityColor = priorityColor,
responseTime = it["timestamps"]["response_time"]?.asInt(),
resolveTime = it["timestamps"]["resolve_time"]?.asInt(),
handleTime = it["timestamps"]["handle_time"]?.asInt(),
dateAdded = it["date"]["added"]?.asInt(),
datePublished = it["date"]["published"]?.asInt()
)
)
}
// var response = jsonMap["response"]
// var mentions = response["data"]
mentionRepo.saveAll(mentions)

How to fix: API call to gmail.users.settings.filters.create failed with error: Empty response

I wrote a script in google apps scripts that creates two filters. I implemented it into web app and it works fine on couple of accounts that aren't that big. But when i try to use it on main account, where it supposed to work I am getting an error that can't really understand.
'API call to gmail.users.settings.filters.create failed with error: Empty response (vers 69, file„createFilter”)'
function createFitler(labelold,me){
var response = Gmail.Users.Labels.list('me');
var labelID = "";
var labelName = "";
for (var i = 0; i < response.labels.length; i++) {
var label = response.labels[i];
if(label.name == labelold)
{
labelID = label.id;
labelName = label.name;
}
}
var filter = Gmail.newFilter();
var filter2 = Gmail.newFilter();
filter.criteria = Gmail.newFilterCriteria();
filter.criteria.from = labelold;
filter2.criteria = Gmail.newFilterCriteria();
filter2.criteria.to = labelold;
filter.action = Gmail.newFilterAction();
filter.action.removeLabelIds = ['INBOX'];
filter.action.addLabelIds = [labelID];
filter2.action = Gmail.newFilterAction();
filter2.action.removeLabelIds = ['INBOX'];
filter2.action.addLabelIds = [labelID];
// Create the filter
Gmail.Users.Settings.Filters.create(filter, me);
Gmail.Users.Settings.Filters.create(filter2, me);
}
The weirdest part is that it works normal on my domain account but not on archive#mydomain.com account.

AegisImplicitMail asp.net html body email

I am trying to send mail in asp.net whit AegisImplicitMail. My code is:
string OggettoEmailIta = "Oggetto email";
string TestoEmailIta = "<p>aaa</p><br/><div>bbb</div>";
var mailMessage = new MimeMailMessage();
mailMessage.Subject = OggettoEmailIta;
mailMessage.IsBodyHtml = true;
mailMessage.Body = TestoEmailIta;
mailMessage.Sender = new MimeMailAddress("XXX", "XXX");
mailMessage.From = new MimeMailAddress("XXX", "XXX");
mailMessage.To.Add(new MimeMailAddress("XXX", "XXX"));
mailMessage.To.Add(new MimeMailAddress("XXX", "XXX"));
var emailer = new SmtpSocketClient();
emailer.Host = "smtp.XXX.XX";
emailer.Port = 465;
emailer.SslType = SslMode.Ssl;
emailer.User = "XXX";
emailer.Password = "XXX";
emailer.AuthenticationMode = AuthenticationType.Base64;
emailer.MailMessage.IsBodyHtml = true;
emailer.MailMessage = mailMessage;
emailer.SendMailAsync();
The mail is sent correctly but it is not in HTML format.
Can someone help me?
you might try string literals "#" for your html message, so all chars are kept
string TestoEmailIta = #"<p>aaa</p><br/><div>bbb</div>";

Store User Control JSON object in DB

I have a JSON object that I want to store in a database. The JSON is generated by an User Control.
The JSON object:
var datatosend = {
isit: isitArray,
ec: ecArray,
bc: bcArray,
bb: bbArray,
io: ioArray
};
What is the best way to record a JSON object in the database using GeneXus?
Edit:
var isitArray = getObjectsRequest('isit', isitID);
var ecArray = getObjectsRequest('ec', ecID);
var bcArray = getObjectsRequest('bc', bcID);
var bbArray = getObjectsRequest('bb', bbID);
var ioArray = getObjectsRequest('io', ioID);
function getObjectsRequest(type, compId){
var array = [];
for (var i = 0; i<compId; i++) {
var comp = mainLayer.find('#'+ type + i)[0];
array.push({
xposition: comp.getX(),
yposition: comp.getY(),
titleid: comp.getId(),
description: comp.find('.textDescription')[0].getText(),
leftrelationsids: comp.leftCRelations,
rightrelationsids: comp.rightCRelations
});
};
return array;
}
var datatosend = {
isit: isitArray,
ec: ecArray,
bc: bcArray,
bb: bbArray,
io: ioArray
};
You can use a LongVarChar attribute to store the JSON, but you'll need to convert the JavaScript object to String first.

HTTPService: What is its send method doing?

I've got a JSON string:
query = {"action":"do","password":"c","name":"s"}
When using HTTPService's send method:
_service = new HTTPService();
_service.url = "http://localhost:8080";
_service.method = "POST";
_service.contentType = "application/json";
_service.resultFormat = "text";
_service.useProxy = false;
_service.makeObjectsBindable = true;
_service.addEventListener(FaultEvent.FAULT,faultRX);
_service.addEventListener(ResultEvent.RESULT,resultRX);
_service.showBusyCursor = true;
var _request:Object = new Object();
_request.query = query;
_service.request = _request;
_service.send();
I don't know what I am doing wrong but on my HTTP server I get:
{["object","Object"]}
Any clues please?
Thanks
You are declaring an object of an object.
Try:
_service.request = query;
_service.send();
you get
{["object","Object"]}
because of this
var _request:Object = new Object();
_request.query = query;
_service.request = _request;
do this
var jsonOBJ:Object = {};
jsonOBJ.action = "do";
jsonOBJ.password = "c";
jsonOBJ.name = "s";
var _service:HTTPService = new HTTPService();
_service.url = "http://localhost:8080";
_service.method = "POST";
_service.contentType = "application/json";
_service.resultFormat = "text";
_service.useProxy = false;
_service.makeObjectsBindable = true;
_service.addEventListener(FaultEvent.FAULT,faultRX);
_service.addEventListener(ResultEvent.RESULT,resultRX);
_service.showBusyCursor = true;
_service.send( JSON.encode( jsonOBJ ) );// encode the json object with AS3Corelib
Don't forget top JSON decode the string on the server side.