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!