0

I'm trying to learn to use the guthub api from Java. I've created a simple program that can read and commit new versions of a file. I have tested this for many text files of short lenght and I think I'm correctly using the mime base64. I'm now trying to upload a larger file, in the order of 5 MB. And this means having a JSON in the body looking like this:

{
"owner": "example42gdrive",
"repo": "Example1",
"message": "FileSystem 42 module on github",
"content": "rO0ABXNyABdpcy5MNDIuZ2V ...5MB of JS string here... ABGluZm90AB5MfqIDcQB+AAU=",
"sha":"a7ef93d3eb50383028578cb916b70060067d9c8a"
}

And I get back as a response

400
{"message":"Problems parsing JSON","documentation_url":"https://docs.github.com/rest/reference/repos#create-or-update-file-contents"}

Notes:

  • The same exact code works for smaller content

  • java Base64.getMimeEncoder() will insert some \n in the result to separate it in lines. I'm removing those newlines in order to get a valid JS string.

Does anyone knows what I'm doing wrong or what should I do instead?

EDIT: after some experimentation, the problem seams to be in the \n: if I produce a base64 string short enough that java Base64.getMimeEncoder() does not insert any \n, all is fine. Of course, a string with a \n can not be 'stringyfied' by simply adding (") before and after, so I tried

  • removing the \n, no effect -> Problems parsing JSON
  • replacing the \n with \n (so that the parser will see them as \n inside a string) -> Problems parsing JSON
  • replacing the \n with \\n (so that the parser will see them as \n, this may help if there are somehow two levels of escape server side ->Problems parsing JSON
  • replacing the \n with a space ->Problems parsing JSON

In https://en.wikipedia.org/wiki/Base64 wikipedia clearly states that

(newlines and white spaces may be present anywhere but are to be ignored
 on decoding)

I'm starting to think that there is something I do not understand and that is so obvious that the github api do not mention it

Marco Servetto
  • 684
  • 1
  • 5
  • 14

1 Answers1

0

Ok, I did it. I found the answer indirectly by reading

Java 8 Base64 Encode (Basic) doesn't add new line anymore. How can I reimplement this?

Basically, just looking to the screen I belived the 'newline' was just "\n", instead it was a "\r\n". Thus, I was replacing only the "\n" leaving the "\r" in place. Replacing "\r\n" with "" works. However, replacing "\r\n" with "\r\n" doesnot. This suggests a bug in the github decoder (if wikipedia is right and new lines must be allowed)

I hate this! I hate that we have more then one way to express 'new line' and that in most context they are rendered the same graphically!!!

Marco Servetto
  • 684
  • 1
  • 5
  • 14