API with matlab.net: Content-Range vs Content-Size

3 views (last 30 days)
Dear all,
I am still a newbie in communication with servers and have the following problem:
I want to upload a file via Matlab API. For simple txt files, my matlab code works. Now, I want to upload a xlsx file (not a large file) and get the server response that the Content-Range is not matching the content size. I have no idea why this happens.
% Initizalization
import matlab.net.*
import matlab.net.http.*
%% Define the API endpoint with GUID
api_endpoint_upload = "https://XXX";
% Select file
[FileName, dataFilePath] = uigetfile('*.*','Select file to upload');
filePath = fullfile(dataFilePath, FileName);
% Get file size
fileInfo = dir(filePath);
fileSize = fileInfo.bytes;
% Get binary data
fid = fopen(filePath, 'rb');
data = char(fread(fid)');
fclose(fid);
% Define the headers for the API request
headers = matlab.net.http.HeaderField('x-api-key', 'YYYY');
headers(2) = matlab.net.http.HeaderField('Content-Type', 'application/octet-stream');
s1 = sprintf('form-data; filename=%s',FileName);
headers(3) = matlab.net.http.HeaderField('Content-Disposition', s1);
s2 = sprintf('bytes 0-%d/%d', fileSize-1, fileSize);
headers(4) = matlab.net.http.HeaderField('Content-Range', s2);
% Define and send post request:
request = matlab.net.http.RequestMessage('post',headers,data);
response = send(request2, URI(api_endpoint_upload));
response.Body.Data.message
% Get the response: Content-Range header size fileSize is not the same as
% the content size (fileSize + XY)

Accepted Answer

Goutam
Goutam on 8 Sep 2025
Hi Simon,
The error is likely caused by how the file data is being read and encoded before transmission
In current code
data = char(fread(fid)');
This converts the raw file bytes into a MATLAB char array, which uses UTF-16 encoding , meaning each character occupies 2 bytes.
As a result:
- dir(filePath).bytes correctly reports the actual file size.
- But the HTTP payload becomes twice as large, since each byte is now represented by a 2-byte character.
- The server receives more data than expected, triggering a mismatch with the content-range header.
Here is a workaround that you may try :
1. Read the file as raw byte.
Use uint8 to preserve the original byte structure:
fid = fopen(filePath, 'rb');
data = fread(fid, Inf, '*uint8').'; % row vector of raw bytes
fclose(fid);
2. Use MATLAB’s FileProvider for streaming.
Let MATLAB handle the file streaming and headers automatically:
import matlab.net.*
import matlab.net.http.*
import matlab.net.http.io.*
provider = FileProvider(filePath); % streams the file
headers = HeaderField('x-api-key', 'YYYY');
request = RequestMessage('POST', headers, provider);response = send(request, URI(api_endpoint_upload));
Kindly refer to the documentation links below for reference :
Hope this helps
Best regards,
Goutam
  1 Comment
Simon
Simon on 8 Sep 2025
Hi Goutam,
your suggestion worked out. Thank you!
Best regards,
Simon

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!