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

8 views (last 30 days)
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?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 24 Jan 2025
Edited: MathWorks Support Team on 4 Feb 2025
Typically, this error occurs when the client (MATLAB) is not able to authenticate successfully with the web server defined by the 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 Authentication 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 cases, 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, you can access the Server Authentication documentation in MATLAB by running the following command in the command window:
>> web(fullfile(docroot, 'matlab/import_export/server-authentication.html'))
For this example, you can modify your code by changing the password field to fix the error:
url = 'http://httpbin.org/basic-auth/foo/bar'; webOpts = weboptions('Username', 'foo',... 'Password', 'bar',... 'Debug', true); out = webread(url, webOpts);
Please follow the link below to search for the required information regarding the current release:

More Answers (0)

Tags

No tags entered yet.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!