Base64 Error: The image contents is not valid base64 data java - java

I am streaming an image to Magento, and encoding an image using android.util.Base64 using either of:
Base64.encodeToString(content, Base64.CRLF)
Base64.encodeToString(content, Base64.DEFAULT)
But I always receive fault:
The image contents is not valid base64 data
Working: I found that the data had to be encoded twice, one time using
Base64 and another encoding using custom Library

Try removing data node from your base64 code for image.
e.g. if you have data like data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVQAAABXCAYAA...
then remove data node. It should look like below and pass it to Magento.
iVBORw0KGgoAAAANSUhEUgAAAVQAAABXCAYAA...

Related

Java-MySQL-Android: bad base-64

I guess this is a stupid question with an obvious solution, but I don't see it yet. So the problem is: I get an IllegalArgumentException on Android, which says my base64 input is not valid. This input took the following way before:
Upload: PDF file -(Java Base64 encoder) > Java Base64 encoded string -(POST)-> PHP -(INSERT as mediumtext via mysqli query)-> MySQL DB
Download: MySQL record -(SELECT via mysqli query and fetch assoc afterwards)-> PHP vars -(JSON)-> Java as JSON -(Jackson library, maps JSON to object containing String)-> Java Base64 String - (Android Base64 decoder)-> Exception
Is there any failure in my workflow? Communication is done with UTF-8 via HttpUrlConnection.
I was able to solve the problem: During transfer to the server + and / got omitted. After manually replacing them, everything is working now.

What is the correct format for the API SurveyQuestionImage.Data field?

I am working with the GCS API, attempting to create a survey with image data.
I am using the NuGet package Google.Apis.ConsumerSurveys.v2 version 1.14.0.564 on the .Net platform. I can create surveys that do not contain image data without problem. However, when I try to create a survey with image data I receive an error from the API.
I have on hand base64 encoded png format image data. My images display properly in an IMG tag on a web page when the src attribute is set to
'data:image/png;base64,<image base64 string>'
I want to send this image data to the API to populate the survey image. My understanding is that I need to set the Data property of the Google.Apis.ConsumerSurveys.v2.Data.SurveyQuestionImage object to a string containing the image data. I have not been successful.
I first decode my base64 string to a byte array:
byte[] bytes = Convert.FromBase64String(<image base64 string>);
I have tried setting the Data property in the SurveyQuestionImage object as:
image.Data = Encoding.Unicode.GetString(bytes);
This results in this error from the API:
Google.Apis.Requests.RequestError Invalid value for ByteString: <the Data string>
I have also tried converting the byte array to a hexadecimal encoded string as:
StringBuilder sb = new StringBuilder(bytes.Length);
foreach (Byte b in bytes)
{
sb.Append(b.ToString("X2"));
}
image.Data = sb.ToString();
This results in the more hopeful error:
Google.Apis.Requests.RequestError Invalid Value supplied to API: image_data was bad. Request Id: 579665c300ff05e6c316a09e600001737e3430322d747269616c320001707573682d30372d32322d72313000010112 [400] Errors [ Message[Invalid Value supplied to API: image_data was bad. Request Id: 579665c300ff05e6c316a09e600001737e3430322d747269616c320001707573682d30372d32322d72313000010112] Location[ - ] Reason[INVALID_VALUE] Domain[global] ]
Does anyone know the correct format for the Data property of the Google.Apis.ConsumerSurveys.v2.Data.SurveyQuestionImage object?
The data needs to be base64 encoded and also "urlsafe" or "websafe" depending on what language you are using. (python and java, respectively)
In other words, you'll need to first base64 encode then:
Web safe encoding uses '-' instead of '+', '_' instead of '/'
Hope this helps!
For c# users, check out this technique for making websafe b64:
How to achieve Base64 URL safe encoding in C#?
For .net users, look at the comments in this question:
Converting string to web-safe Base64 format
And also this link for more info about .net specific options for encoding:
http://www.codeproject.com/Tips/76650/Base-base-url-base-url-and-z-base-encoding
And to specifically answer the original poster, try this for converting your byte array to a string.
public static string ToBase64ForUrlString(byte[] input)
{
StringBuilder result = new StringBuilder(Convert.ToBase64String(input).TrimEnd('='));
result.Replace('+', '-');
result.Replace('/', '_');
return result.ToString();
}

IOS Receipt validation IllegalArgumentException

I use similar code as its shown here in the question.
Java and AppStore receipt verification
But I still end up getting
{"status":21002, "exception":"java.lang.IllegalArgumentException"}
Can it be a problem at Base64 encoding?. Do I have to convert the base64 encoded string into hex or something else?.
What i post is similar to following
{"receipt-data" : "eyJzaWduYXR1cmUiOiJBbjNJVER0VVNmZWNhaGMxR.....
The problem was at Base64 encoding inside Java. When I do the encoding inside IOS and use that as the request from server without any encoding in Java, then it worked.
I had a similar problem and was receiving the java.lang.IllegalArgumentException from Apple when trying to validate a receipt on my server. The problem was that my base64 encoding logic was inserting lines breaks into the encoded string. Once I updated my code to ensure no new line breaks were being inserted into the encoded string, I was able to successfully verify my receipts against Apple's servers.

Java servlet json object containing XML, encoding problems

I have a servlet which should reply to requests in Json {obj:XML} (meaning a Json containing an xml object inside).
The XML is encoded in UTF-8 and has several chars like => पोलैंड.
The XML is in a org.w3c.dom.Document and I am using JSON.org library to parse JSON. When i try to print it on the ServletOutputStream, the characters are not well encoded. I have tested it trying to print the response in a file, but the encoding is not UTF-8.
Parser.printTheDom(documentFromInputStream,byteArrayOutputStream);
OutputStreamWriter oS=new OutputStreamWriter(servletOutputStream, "UTF-8");
oS.write((jsonCallBack+"("));
oS.write(byteArrayOutputStream);
oS.write(");");
I have tryed even in local (without deploing the servlet) the previous and the next code :
oS.write("पोलैंड");
and the result is the same.
Instead when I try to print the document,the file is a well formed xml.
oS.write((jsonCallBack+"("));
Parser.printTheDom(documentFromInputStream,oS);
oS.write(");");
Any help?
Typically, if binary data needs to be part of an xml doc, it's base64 encoded. See this question for more details. I suggest you base64 encode the fields that can have exotic UTF-8 chars and and base64 decode them on the client side.
See this question for 2 good options for base64 encoding/decoding in java.

SOAP: Reading SOAP response's embedded file

In SOAP Client application. I am using javax.xml.soap api. I am getting the soap response. A part of it, shown below.
<ns5:XXX type="Full" format="HTML">
<ns5:EmbeddedFile MIMEType="text/html"
fileExtension="html"
fileName="ZZZ.html">
<ns5:Document>...</ns5:Document>
</ns5:EmbeddedFile>
</ns5:XXX>
The value between the Document tag is in the Base64 format.
I need to know two things, as in the above code you will see that, the fileName is zzz.html.
where this zzz.html file will stored or exits. I search for in my local machine i do not find.
Another thing i would like to know that the between the Document tags it show long text messages in the Base64 format. Is this is the document that exists in the zzz.html. If it is so how to read that document.
Thanks
This appears to be a custom way of embedding file content to a SOAP message being used by the service you are calling - a standard way of doing this would have been using Soap Attachments.
In this specific case, it does look like the file content is being embedded as Base64 data between the Document tags, and the meta information of the file is the attributes of EmbeddedFile tag. You will basically have to decode the Base64 encoded content - see here
and here on how to, move the contents to a file with the name in the meta information tag.

Categories