Communication with server via API
Ältere Kommentare anzeigen
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
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Web Services finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!