Communication with server via API

조회 수: 9 (최근 30일)
Tom Bernand
Tom Bernand 2023년 3월 2일
댓글: Tom Bernand 2024년 5월 24일
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

채택된 답변

Chetan
Chetan 2024년 5월 10일
편집: Chetan 2024년 5월 10일
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!
  댓글 수: 1
Tom Bernand
Tom Bernand 2024년 5월 24일
Hello Chetan,
Thank you very much for your help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 JSON Format에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by