Clear Filters
Clear Filters

Communication with server via API

8 views (last 30 days)
Hello all,
I am still a newbie in communication with servers and have the following problem:
I want to automatically translate text from a database using a translator. For this I want to use the LibreTranslator (Link).
LibreTranslate directly provides an example of a query in JavaScript:
const res = await fetch("https://de.libretranslate.com/translate", {
method: "POST",
body: JSON.stringify({
q: "",
source: "auto",
target: "de",
format: "text",
api_key: ""
}),
headers: { "Content-Type": "application/json" }
});
console.log(await res.json());
Now I would like to implement this request in MATLAB. For this I have resorted to webwrite and tried to write a query as in this Example.
The content of my function is as follows:
api = 'https://de.libretranslate.com/translate';
body.q= "";
body.source = "auto";
body.target = "de";
body.format = 'text';
body.api_key = '';
if ~exist('options','var')
options = weboptions;
end
options.RequestMethod="post";
options.MediaType = "application/json";
s = webwrite(api,body,options);
When I run this now I get the following error:
---------------------------------------------------------------------------------------------------------------------------------------------------
Error using matlab.internal.webservices.HTTPConnector/copyContentToByteArray
The server returned the status 500 with message "Internal Server Error" in response to the request to URL
https://de.libretranslate.com/translate.
---------------------------------------------------------------------------------------------------------------------------------------------------
I have also already tried to create a json file that has exactly the structure from the example query and pass this. The result remained the same.
{
"methods": "POST",
"body": {
"q": "Hello",
"source": "auto",
"target": "de",
"format": "text",
"api_key": ""
},
"headers": {
"Content-Type": "application/json"
}
}
I would be very happy if someone could help me here.
Many thanks and many greetings
Tom

Accepted Answer

Chetan
Chetan on 10 May 2024
Edited: Chetan on 10 May 2024
It appears you're encountering a "500 Internal Server Error" while trying to send a POST request to the LibreTranslate API for text translation from MATLAB, following a JavaScript example.
To tackle this in MATLAB, consider the following steps:
  • JSON Formatting: Make sure your data structure aligns with what the API expects. MATLAB's `webwrite` function automatically converts structures to JSON
  • Headers and Options: You correctly set the `MediaType` to `"application/json"`. Ensure you check and include any other necessary headers or options.
  • API Key: If the API demands an API key, verify its validity and include it in your request.
Here's a revised code snippet:
api = 'https://de.libretranslate.com/translate';
body = struct('q', "Hello", 'source', "auto", 'target', "de", 'format', 'text', 'api_key', '');
options = weboptions('RequestMethod', 'post', 'MediaType', 'application/json', 'Timeout', 30);
try
response = webwrite(api, body, options);
disp(response);
catch ME
disp('Error occurred:');
disp(ME.message);
end
detectedLanguage: [1x1 struct] translatedText: 'hallo'
Refer to the following MathWorks Documentation for more details:
Hope this helps!

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!