Translating curl into Matlab/Webwrite

12 views (last 30 days)
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"

Accepted Answer

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

More Answers (2)

Paolo
Paolo on 17 May 2018
Edited: Paolo on 17 May 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 on 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.

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!