Can I make the matlab http send function to use "%40" in the destination url?

121 views (last 30 days)
I need to send a PUT or POST request to a url containing '%40' (encoded "@" symbol) in the destination url. I need to use the matlab.net.http send function in order to get information from the response headers.
I am not an expert on http. My problem is that regardless how I try to structure the message and send function, the sent request either includes the unencoded "@" symbol or the double-encoded '%2540', both of which trigger errors from the server.
Is there a way to force the send function to use '%40' in the destination address?
I am providing a dummy example below to show the issue because the real url (from Adobe API) is extremely long and contains API keys.
Grateful for any help on this.
import matlab.net.*
import matlab.net.http.*
% URL- NOTICE THE REQUIRED ENCODED %40 which is encoded symbol "@"
exampleURL = 'https://www.example.com/abcd%40efgh.com/ijkl?queryString=queryValue';
exampleURL_components = URI(exampleURL,'literal');
% Construct a dummy request message. This does not depend on the url.
exampleRequest = RequestMessage('PUT',[], "Irrelevant");
% Send the dummy request to original URL - The symbol "@" is DOUBLE ENCODED - causes error
[~,sentrequest,~] = send(exampleRequest,exampleURL);
disp('Sent request contains double-encoding from @ to %2540');
disp(sentrequest.RequestLine.RequestTarget);
% Send the dummy request to URI - The symbol "@" is UNENCODED - causes error
[~,sentrequest2,~] = send(exampleRequest,exampleURL_components);
disp('Sent request contains unencoded @');
disp(sentrequest2.RequestLine.RequestTarget);
  1 Comment
Assen Koev
Assen Koev on 11 Nov 2024 at 21:46
Edited: Assen Koev on 11 Nov 2024 at 21:46
A note:
It appears to me the behavior I described above comes from the internal URI function which uses
path = urlencode(obj.Path, obj.Pchar) %line 499
where the Pchar are characters that are allowed to pass unencoded and include "@". So this causes '%40' to be double encoded and '@' to be left unencoded, i.e. not allowing for '%40' to be included in the sent message. Of course, I still don't know how to get around the noted behavior.

Sign in to comment.

Answers (1)

Sreeram
Sreeram on 18 Nov 2024 at 5:43
MATLAB’s HTTP interface does not provide an option to disable encoding or force the “send” function to use a URL. The “send” function ensures that the URL is valid according to the encoding rules.
According to RFC 3986, the path of a URI may consist of characters in ‘segment-nz-nc’ and this set lists the ‘@’ character explicitly:
segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
So, the HTTP URI displayed in the last line of the code you shared is valid. You might want to consider double checking why this causes the error.
Thanks
  1 Comment
Assen Koev
Assen Koev on 20 Nov 2024 at 20:17
Thanks @Sreeram and appreciate the reference.
The problem I am facing is a result of factors:
  1. The web-address is provided with %40 (by an Adobe API) and placing "@" in it throws an error. I don't have any control over the address formulation.
  2. The urlencode function is not only keeping "@" (as per standard as you say) but actively replaces %40 with "@"
So, I am looking for a way to allow %40 to remain in the address if so specified.

Sign in to comment.

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!