Borrar filtros
Borrar filtros

How to create http MessageBody body with fields that contain dashes

53 visualizaciones (últimos 30 días)
I would like to create http message bodies with fields that contain dashes. I haven't been able to find a workaround for Matlab disallowing hyphens (dashes) in the var names.
Here is what I'm trying to do - for illustration - I have an underscore in place of a hyphen the "test_var" (below)
Even after generating the RequestMessage (req1 below), you cannot change "test_var" to "test-var" because the RequestMessage is still storing the request as a struct (not as a string which could be modified).
method = matlab.net.http.RequestMethod.POST;
header = matlab.net.http.HeaderField('Content-Type', 'application/json');
header = addFields(header,'User-Agent', 'my-client/99.9');
header = addFields(header,'Accept', 'application/json');
% Struct approach
struct1 = struct('login', 'me', 'password', 'pw123', 'test_var', true)
body1 = matlab.net.http.MessageBody(struct1)
req1 = matlab.net.http.RequestMessage(method,header,body1)
show(req1)
POST
Content-Type: application/json
User-Agent: my-client/99.9
Accept: application/json
{"login":"me","password":"pw123","test_var":true}
req1.Body.Data
ans =
struct with fields:
login: 'me'
password: 'pw123'
test_var: 1
  10 comentarios
dpb
dpb el 21 de Jul. de 2024 a las 19:47
Will Python support? Maybe there's a way there that could be called from MATLAB. Or, use the .NET stuff directly, maybe? I dunno, I'm just fishing here.
As noted, I'd either send a support request or call directly on this one.
stedst9
stedst9 el 21 de Jul. de 2024 a las 22:44
Editada: stedst9 hace alrededor de 21 horas
Yes @dpb - I've had the same thought: Python would be a last resort work-around. It seems like such a hack though.
In theory, the Mathworks deverlopers put enough capability into the MessageBody class that there should be at least one way to do it in Matlab. I'm hoping to get a good, clean Matlab solution and maybe some improvements to the online Matlab documentation that show how to use more of the options in the MessageBody class.

Iniciar sesión para comentar.

Respuesta aceptada

stedst9
stedst9 hace 1 minuto
I contacted Mathworks support and received the following solution - it works:
keySet = {'login', 'password', 'test-var'};
valueSet = {'me', 'pw123', true};
map = containers.Map(keySet, valueSet);
jsonMap = jsonencode(map);
>> jsonMap = jsonencode(map)
jsonMap =
'{"login":"me","password":"pw123","test-var":true}'

Más respuestas (1)

Samuel Chan
Samuel Chan hace alrededor de 13 horas
If Content-Type can be "text/plain" (mislabelling the content-type -- not sure if your API accepts that), then you can use string or char array as the message body:
MessageBody('{"login":"me","password":"pw123","test-var":true}');
If Content-Type must be "application/json", then calling RequestMessage.send will use jsonencode on your message body to generate the payload to send. If your message body contains a struct, you get:
'{"login":"me","password":"pw123","test_var":true}'
else if your message body contains a string, you get:
"{\"login\":\"me\",\"password\":\"pw123\",\"test-var\":true}"
Neither result is the JSON with dashed fields you desired.
If you want both Content-Type "application/json" and dashed field, you may look into this:
"To send arbitrary headers and data in a request message, set Completed to true to prevent the send method from modifying the message."
But in that case, it seems you will have to build and validate the request message yourself. (I have not try this before.)
  1 comentario
stedst9
stedst9 hace alrededor de 6 horas
Samuel,
I'm not permitted to mislabel the content-type. You're right about the struct and string output - you got the same result as me. For the past few hours, I've made several of RequestMessage attempts using request.Completed = true. Still no luck - I'm not through the woods yet.

Iniciar sesión para comentar.

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by