Error using webwrite (line 136), Could not access server.
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
amador zuniga
el 20 de Nov. de 2023
Comentada: amador zuniga
el 5 de Dic. de 2023
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.
0 comentarios
Respuesta aceptada
Dheeraj
el 1 de Dic. de 2023
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 “webwrite” better understanding.
Hope this Helps!
1 comentario
Más respuestas (0)
Ver también
Categorías
Más información sobre JSON Format en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!