Translating curl into Matlab/Webwrite
12 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
その他の回答 (2 件)
Paolo
2018 年 5 月 17 日
編集済み: Paolo
2018 年 5 月 17 日
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 件のコメント
sherhraza
2018 年 11 月 23 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Introduction to Installation and Licensing についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!