Error using webwrite (line 136), Could not access server.

조회 수: 11 (최근 30일)
amador zuniga
amador zuniga 2023년 11월 20일
댓글: amador zuniga 2023년 12월 5일
I'm running matlab R2019b in an EC2 AWS instance.
I am using webwrite to make a post request through an API.
My code has been working long time with an old URL, but it fails with the new one. (We are migrating to a new domain)
'http://ip-number1-number2-number3-number4-number5.YYYY.compute.internal/v1/measurements' % it works
'http://name0.name1.name2-name3.com/v1/measurements' % it works
'http://name0.name1.name2/v1/measurements'; % it doesn't work
My code (quick test) is:
URL = 'http://ip-number1-number2-number3-number4-number5.YYYY.compute.internal/v1/measurements';
API_KEY = '0L03GF498GT3486GMN34T7G5N2DX2C380U4TV329M'; % not real api_key
DATA = ['"TIMESTAMP","VALUE"' char(10) ...
'"2023/11/15 00:30:00","6577.40"' char(10) ...
'"2023/11/15 01:00:00","6482.62"'];
VALUES = struct("apiKey", API_KEY, ...
"values", struct("dataVariable", DATA));
VALUES.values.dataElement = {VALUES.values.dataElement};
VALUES.values = {VALUES.values};
VALUES = jsonencode(VALUES);
% Options
OPTIONS = weboptions('Timeout', 60, 'HeaderFields',{'Content-Type' 'application/json'});
% Send POST request
[response] = webwrite(URL, VALUES, OPTIONS)
when the code works, it returns:
response =
struct with fields:
codError: 0
requestId: '823459781112'
valuesCount: [1×1 struct]
when it doesn't:
Error using webwrite (line 136)
Could not access server. http://name0.name1.name2-name3.com/v1/measurements.
It doesn't return 401, 404 error or gives any extra information.
I have no idea how to solve it.
I discarted AWS configuration, because when I run the query using a web browser it works and returns a json.
Maybe matlab manage internaly a white-list URL, SSL certificates or something like that.

채택된 답변

Dheeraj
Dheeraj 2023년 12월 1일
Hi,
I understand that you are encountering an error when trying to contact a host using webwrite. This error might indicate issues with DNS resolution.
The error you're encountering is because MATLAB's webwrite function is using an outdated hostname when attempting to make the POST request to the new domain. When you provide the hostname as 'http://name0.name1.name2-name3.com/v1/measurements', MATLAB tries to resolve it using an internal DNS server that doesn't have the updated record for the new domain. This prevents MATLAB from establishing a connection to the server and result in the "Could not access server" error.
To resolve this issue, you can explicitly specify the IP address of the new domain in the URL string. This will bypass MATLAB's internal DNS resolution and ensure that the request is sent to the correct server.
If the steps did not seem to work and result in the same error, you should validate the SSL certificate of the instance.
You could refer to the below MATLAB documentation on webwritebetter understanding.
Hope this Helps!
  댓글 수: 1
amador zuniga
amador zuniga 2023년 12월 5일
I just solved it including "www" and ".com" to the url.
'http://name0.name1.name2/v1/measurements'; % it doesn't work
'http://www.name0.name1.name2.com/v1/measurements'; % it works
thanks.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by