Curl translation to webwrite()

6 views (last 30 days)
Karsten Kähler
Karsten Kähler on 14 Apr 2021
Edited: Rupesh on 20 May 2024
See edit at end for solution; tldr: jsonencode() does not create arrays for single values.
I need to send data in JSON to a webserver and it will generate some data that is send back (also JSON).
The following curl command works:
curl -X POST -H "Content-Type: application/json" -d @minimal_request.json http://x.yz/ > minimal_output.json
The sent data is generated in Matlab and the received data is further processed in Matlab. For automatisation I tried webwrite() instead of the (manual) curl:
file = 'minimal_request.json';
URL = 'http://x.yz/';
str = fileread(file);
json_struct = jsondecode(str); %data will later be delivered directly in json_struct
options = weboptions('MediaType','application/json', 'ContentType', 'json');
res = webwrite(URL, json_struct, options);
But this will produce the following error message (indicating a bad request)
Error using matlab.internal.webservices.HTTPConnector/copyContentToByteArray (line 373)
The server returned the status 400 with message "" in response to the request to URL
http://x.yz/.
Error in readContentFromWebService (line 46)
byteArray = copyContentToByteArray(connection);
Error in webwrite (line 139)
[varargout{1:nargout}] = readContentFromWebService(connection, options);
Error in TestV2 (line 9)
res_PWT = webwrite(URL, json_struct, options);
I can't seem to find the problem here...
Thanks!
~edit for future~
The server expected JSON-arrays even if there is only a single value to be send. But jsonencode() (which is called in webwrite() ) does not create arrays for single values.
Workaround is to explicitly use a cell-struct; e.g.
s.value{1,1} = 1
instead of
s.value = 1

Answers (1)

Rupesh
Rupesh on 20 May 2024
Edited: Rupesh on 20 May 2024
Hi Karsten
I understand the issue you've encountered. The problem arises when using “webwrite()” in MATLAB to send JSON data to a server. The error you're facing suggests a bad request (HTTP 400), which is likely due to the format of the JSON data sent. Specifically, the server expects arrays in the JSON payload, even for single values, but MATLAB's “jsonencode()” function, which is implicitly called by “webwrite()”, does not wrap single values in arrays. This discrepancy between the expected and actual data formats leads to the error.
To solve the problem, you can manually ensure that all values intended to be arrays are explicitly defined as cell arrays in MATLAB, even if they contain only a single element. You can try below steps to apply this workaround:
  • Prepare the Data: Before encoding your data as JSON, for any value that should be sent as an array to the server, use a cell array to represent it, even if it contains only a single value. For example, instead of assigning a single value directly like s.value = 1, you should wrap the value in a cell array: s.value{1,1} = 1.
  • Encode and Send the Data: After adjusting the data structure, you can proceed with using “webwrite()” as you normally would. The “jsonencode()” function will now correctly encode these cell arrays as JSON arrays, matching the server's expectations. Here's a brief example of how you might encode and send the data:
options = weboptions('MediaType','application/json', 'ContentType', 'json');
res = webwrite(URL, json_struct, options);
%In this code, “json_struct” should be the structure where [you've applied the cell array workaround. By ensuring all values that the server expects as arrays are indeed represented as arrays in your JSON payload, you should be able to avoid the HTTP 400 error and successfully communicate with the server. ' ...
%'You can use the below resource for better understanding of server-side communication.]
Hope it helps!

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!