What causes "webread" and "webwrite" to return status 401?

37 visualizaciones (últimos 30 días)
What is the cause of the error:
The server returned the status 401 with message "<message from server>" in response to the request to URL <url>”
and how do I fix it?

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 23 de Nov. de 2020
Typically, this error occurs when the client (MATLAB) is not able to authenticate successfully with the web server defined by URL. The web server requires the client to authenticate itself for the request to be successful.
Typical reasons why the request might not succeed are:
  1. The web server requires an authentication scheme which MATLAB does not support.
  2. The MATLAB user provided invalid user credentials (e.g. username and password).
  3. The web server does not provide access to certain users.
  4. If the 401 status has started appearing after an upgrade to MATLAB R2019b, consider adding a Basic Authentification header as described here: https://uk.mathworks.com/matlabcentral/answers/480026-how-do-i-preemptively-include-a-basic-authentication-header-when-working-with-webread-webwrite
This code shows how a MATLAB user can find the authentication scheme that the server requires. The examples demonstrate this error with a GET request. The first example uses the RESTful function webread. The second example uses the GET request in the HTTP interface. In both case, the user supplied incorrect credentials, specifically an incorrect password.
For RESTful functions:
url = 'http://httpbin.org/basic-auth/foo/bar';
webOpts = weboptions('Username', 'foo',...
'Password', 'bar123',...
'Debug', true);
try
out = webread(url, webOpts);
catch exc
disp(exc);
end
For HTTP Interface:
import matlab.net.*;
import matlab.net.http.*;
import matlab.net.field.*;
uri = URI('http://httpbin.org/basic-auth/foo/bar');
httpOpts = HTTPOptions;
cred = Credentials ('Username', 'foo', 'Password', 'foo123');
httpOpts.Credentials = cred;
r = RequestMessage('GET');
try
[resp, req, hist] = r.send(uri, httpOpts);
catch exc
disp(exc);
end
show(hist, 0);
When you run the examples, MATLAB displays the response from the server in the command window. This screenshot shows the response for these examples.
Since the password is incorrect, the server returns a “401” status code with a message “UNAUTHORIZED”.
You can search for the field “WWW-Authenticate:” to see what authentication scheme is requested by the server. In this case, the server requests “Basic” authentication scheme, which requires you to supply a username and password.
If a scheme is not supported by MATLAB, then you will not be able to complete your request successfully. For more information on the list of supported authentication schemes see Server Authentication in MATLAB documentation:
For this example, you can modify your code, by changing the password field to fix the error:\n
url = 'http://httpbin.org/basic-auth/foo/bar';
webOpts = weboptions('Username', 'foo',...
'Password', 'bar',...
'Debug', true);
out = webread(url, webOpts);

Más respuestas (0)

Categorías

Más información sobre Call Web Services from MATLAB Using HTTP en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by