Translating curl into Matlab/Webwrite
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Frank Schmull
el 14 de Mayo de 2018
Respondida: sherhraza
el 23 de Nov. de 2018
I have the following curl command I need to sent to a web server using Matlab and webwrite using POST. My problem is that I always get a "Bad request" answer so my syntax must be wrong somehow. Does anybody have an idea how this curl command, sending the body could look like in Matlab using webwrite in a correct way ?
body=$(cat << EOF
{
"order": {
"units": "100",
"instrument": "EUR_USD",
"timeInForce": "FOK",
"type": "MARKET",
"positionFill": "DEFAULT"
}
}
EOF
)
curl \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <AUTHENTICATION TOKEN>" \
-d "$body" \
"https://api-fxtrade.oanda.com/v3/accounts/<ACCOUNT>/orders"
0 comentarios
Respuesta aceptada
Más respuestas (2)
Paolo
el 17 de Mayo de 2018
Editada: Paolo
el 17 de Mayo de 2018
Hi Frank,
Weboptions allows you to add headers to the POST request.
%Body of POST request.
data = struct('units',100,'instrument','EUR_USD','timeInForce','FOK',...
'type','MARKET','positionFill','DEFAULT');
%Options for request.
options = weboptions('RequestMethod','POST','MediaType','application/json',...
'KeyName','Authorization: Bearer','KeyValue','abc');
server_url = 'https://api-fxtrade.oanda.com/v3/accounts/<ACCOUNT>/orders';
response = webwrite(server_url,data,options);
The code should allow you to successfully complete a POST request. If you are still receiving a 'Bad Request' reply it is possible that the server is not configured as you think it is.
0 comentarios
sherhraza
el 23 de Nov. de 2018
Hi,
I have a similar problem. I want to convert the following command from GIT BASH to a local server in my company to matlab.
$ curl -X POST http://<ServerName> -d '{"variables":{"$URL":{"stringArray":{"values":["corbaname::"abcName.ASAM-ODS"]}}}}' --header 'Accept: application/x-asamods+json' --header 'Content-Type: application/x-asamods+json' -u username:password -i
This works on GIT Bash as i get a 201 return message with the required data.
I want to shift this to Matlab.
This is what i have so far
url = 'http://<ServerName>';
data = '{"variables":{"$URL":{"stringArray":{"values":["corbaname::#abcName.ASAM-ODS"]}}}}}'
options = weboptions('RequestMethod','POST','MediaType','application/json','HeaderFields',{'Content-Type:' 'application/x-asamods+json'; 'Accept' 'application/x-asamods+json' });
data = webwrite(url,data,options);
From Matlab i get an error code 400 for a bad request.
Any ideas on what i am doing wrong? Any help will be greatly appreciated.
0 comentarios
Ver también
Categorías
Más información sobre Introduction to Installation and Licensing en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!