MATLAB: Do I get the errror Invalid character code sequence detected when I use ‘£’ or any other non-ASCII character in username or password in weboptions

asciicharactercodedetectedinvalidMATLABsequenceweboptions

I am trying to use a webservice where my password contains a £ character but I get an error like: 
Caught "std::exception" Exception message is: 
Invalid character code sequence detected. 
This error is unusual as it doesn't provide a stack trace When I remove the £ symbol I do not get the error. 
You can recreate the error like this (you don't need a valid web service). 
 
% Normal 
options = weboptions('Username', 'someusername', 'Password', 'letmein', 'MediaType', 'application/json'); 
webwrite('http://example.com', struct(), options); 
% Add £ to password and get Error
options = weboptions('Username', 'someusername', 'Password', 'letmein£', 'MediaType', 'application/json'); 
webwrite('http://example.com', struct(), options);
I currently work around the issue by changing my password to one that does work but this is inconvenient and may not always be possible. 
 

Best Answer

I understand that you are trying to use a webservice and when password contain £ character in weboptions, you get the following error while using webwrite-
Invalid character code sequence detected.
This error message was thrown because '£' is not a valid ASCII character(It is part of ISO/IEC 646). Currently only ASCII characters are supported.
As a workaround, you can use Base 64 encoding after converting Unicode character representation to numeric bytes-
wo = weboptions('header',{'Authorization',['Basic ' matlab.net.base64encode(unicode2native('someusername:letmein£','utf8'))]}, 'MediaType', 'application/json')
webwrite('http://example.com', struct(), wo);
This will allow you to use non-ASCII characters like '£' in weboptions.