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

89 次查看(过去 30 天)
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 个评论
Assen Koev
Assen Koev 2024-11-11,21:46
编辑:Assen Koev 2024-11-11,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.

请先登录,再进行评论。

回答(1 个)

Sreeram
Sreeram 2024-11-18,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

类别

Help CenterFile Exchange 中查找有关 Call Web Services from MATLAB Using HTTP 的更多信息

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by