I've built a small HTML/JS page that uses GitLab API to upload files into the Package Registry like:
<input class="form-control mb-1" type="file" id="formFile" />
<button class="btn btn-primary w-100" onclick="Send();">
<script>
function Send() {
...
var formData = new FormData();
formData.append("file", select_file);
fetch(file_string, {
method: "PUT",
headers: {
"PRIVATE-TOKEN": GITTOKEN,
},
body: formData,
})
.then((response) => response.json())
.then((data) => {
...
I've noticed that, for some reason, if I upload a text file like crc.txt which contains just a CRC code like ac8daadc, GitLab seems to write the payload/content of the file like:
------WebKitFormBoundaryVPZEUt253yBGuPTN
Content-Disposition: form-data; name="file"; filename="crc.txt"
Content-Type: text/plain
ac8daadc
------WebKitFormBoundaryVPZEUt253yBGuPTN--
Why is that? Can I keep the original payload?
It turned out that the issue was on the var formData = new FormData();formData.append("file", select_file);
code.
The formData not only upload the data, but serialize other information into the file, corrupting it.
Passing the file directly with
fetch(file_string, {
method: "PUT",
headers: {
"PRIVATE-TOKEN": GITTOKEN,
},
body: select_file,
})
worked