API with matlab.net: Content-Range vs Content-Size
Show older comments
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
More Answers (0)
Categories
Find more on Call Web Services from MATLAB Using HTTP in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!