JSON POST API CALL FROM MATLAB ( Diffchecker API )
17 次查看(过去 30 天)
显示 更早的评论
Hi,
I am trying to fetch the data from the Diffchecker API (https://www.diffchecker.com/public-api/)
This is their usage example:
curl --location --request POST 'https://api.diffchecker.com/public/text?output_type=html&email=YOUR_EMAIL' \
--header 'Content-Type: application/json' \
--data-raw '{
"left": "roses are red\nviolets are blue",
"right": "roses are green\nviolets are purple",
"diff_level": "word"
}'
Now I can not get this to work in Matlab. I have tried every webwrite() solution that I could find here on Mathworks, like the following from https://se.mathworks.com/matlabcentral/answers/400676-translating-curl-into-matlab-webwrite#answer_320645:
%Body of POST request.
data = struct('output_type',"html",'email',"my.email@gmail.com",'diff_level',"word",...
'left',"hej hej hej",'right',"nej nej hej");
%Options for request.
options = weboptions('RequestMethod','POST','MediaType','application/json');
server_url = 'https://api.diffchecker.com/public/text';
response = webwrite(server_url,data,options);
And also the variation:
url = 'https://api.diffchecker.com/public/text?output_type=html&email=my.email@gmail.com';
options = weboptions('RequestMethod','post', 'MediaType','application/json');
Body = struct('left', 'Hey hey hey','right', 'Hey yo yo');
Body = struct('order', Body);
response = webwrite(url, Body, options);
None of this works as I the server returns status 400 "Bad Request".
Please help me.
3 个评论
Yogita
2023-5-29
To make a JSON POST API call to the Diffchecker API from MATLAB, you can use the webwrite function with the appropriate parameters. However, it seems that you are encountering a "Bad Request" error when making the API call. To troubleshoot and resolve this issue, let's break down the problem and provide a potential solution.
First, it's important to note that the Diffchecker API example you provided is using cURL syntax. We need to convert it to MATLAB syntax using webwrite. Here's an updated version of your MATLAB code:
% Define the API endpoint URL
url = 'https://api.diffchecker.com/public/text';
% Set the request parameters
params = struct('output_type', 'html', 'email', 'YOUR_EMAIL');
data = struct('left', 'roses are red\nviolets are blue', ...
'right', 'roses are green\nviolets are purple', ...
'diff_level', 'word');
payload = struct('data', data, 'params', params);
% Specify the options for the request
options = weboptions('RequestMethod', 'post', 'MediaType', 'application/json');
% Make the API call
response = webwrite(url, payload, options);
In this code, we define the API endpoint URL as url and set the request parameters in the params and data structures. We then create a payload structure containing both the parameters and data. Finally, we make the API call using webwrite, passing the url, payload, and options.
If you are still receiving a "Bad Request" error, it's possible that the issue lies with the format of the request body. To troubleshoot further, you can use the online diff checker tool, which you mentioned in your question, to compare the expected request body with the one generated by your MATLAB code. Here's the link to the diff checker tool: Diff Checker.
Copy the request body generated by your MATLAB code and the expected request body from the API documentation into the tool. It will highlight any differences between the two, helping you identify any discrepancies or missing parameters.
By using the diff checker tool, you can compare the expected request body with the one generated by your MATLAB code and pinpoint any differences. This should assist you in resolving the "Bad Request" issue and successfully making the JSON POST API call from MATLAB.
回答(1 个)
Abhijeet
2023-9-1
Hi Emil,
I can understand that you want to fetch data from “Diffchecker API”, but you are getting status 400 “Bad Request” error.
Please refer to the following working MATLAB code for fetching the data from “Diffchecker API”.
url = 'https://api.diffchecker.com/public/text?output_type=html&email=YOUR_EMAIL';
data = struct(...
'left', 'roses are red\nviolets are blue', ...
'right', 'roses are green\nviolets are purple', ...
'diff_level', 'word' ...
);
options = weboptions('MediaType', 'application/json', 'RequestMethod', 'post');
response = webwrite(url, data, options);
disp(response)
For more information on ‘webwrite’ function please refer to the following documentation: - https://in.mathworks.com/help/matlab/ref/webwrite.html
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 JSON Format 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!