Translating curl into Matlab/Webwrite

14 visualizzazioni (ultimi 30 giorni)
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"

Risposta accettata

Frank Schmull
Frank Schmull il 17 Mag 2018
Thank you very much for your answer. I think I will check in addition the server configuration with the provider.

Più risposte (2)

Paolo
Paolo il 17 Mag 2018
Modificato: Paolo il 17 Mag 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.
Alternatively, you can just run 'curl' from within Matlab using system.

sherhraza
sherhraza il 23 Nov 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.

Categorie

Scopri di più su Introduction to Installation and Licensing in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by