New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[bug] http fetch fails to send form with correct image encoding #4312
Comments
Note: an example server to test this aganst is Jira. Run, setup a project, create an issue: Then post an attachment to an issue: |
Can you try changing the content-type header to |
@lucasfernog sorry I'm not understanding, don't I already show the Also, I add the following headers to the request (in addition to auth):
Keep in mind I show that this works with specifying a path for |
ahh you have the feature, sorry didn't see that. I'll investigate this issue, thanks for the report. |
@evbo I just tried it and it work both with a file path and a byte array. Here's my code: const file = await readBinaryFile('/path/to/image.png')
result = await fetch('http://localhost:8080/rest/api/2/issue/TEST-25/attachments', {
method: 'POST',
body: Body.form({
'file': {
file, mime: 'image/png', fileName: 'image.png'
}
}),
headers: { 'Content-Type': 'multipart/form-data', Authorization: 'Basic bHVjYXNmZXJub2c6MDQwODk2NTA0MjI=', 'X-Atlassian-Token': 'no-check' }
}) Note that if you use Also, the multipart logic is the same when using a file path or a byte array (internally we just read the file and get its bytes to send), so both ways should work. If you want me to try something else, please share some code with us. |
Actually there's an issue when using |
Also found the issue for the reqwest usage, thanks for catching that :) |
Thanks @lucasfernog and @FabianLars I'm glad I provided some assistance in the end! Now I wish I read this sooner! Here's the proper way to encode files as byte arrays using FileReader in JS: There's a lot of examples online instructing to read files as URLs or Text - that's only useful for HTML tags like |
Describe the bug
If you call the http
form
function to produce a Body that includes both a Byte arrayfile
the correct mime type isn't used and what gets uploaded is a unicode text file instead of an image.Example:
Reproduction
Call the
fetch
api with a body created byBody.form(...)
with an end result similar to:Where "someKey" is what your server expects to be the key in the form containing the file and
byteArray
is an array of numbers, each number between (-128, 127).An example byte array (in plain text) is attached (for the Tauri logo):
The file will be correctly uploaded, however it will simply be a text file, not binary (image) if in linux you run the command
file -i img.png
it will show a mime type of plain text.As a control simply perform the upload specifying an absolute path to the image file:
And this works great! So we know it is only an issue when supplying a byte array as
file
Expected behavior
The file should have mime type of
image/png
Platform and versions
Stack trace
No response
Additional context
I tried adding
reqwest-client
but that resulted in it completely not uploading at all with my server rejecting:The text was updated successfully, but these errors were encountered: