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"

采纳的回答

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

更多回答(2 个)

Paolo
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.
Alternatively, you can just run 'curl' from within Matlab using system.

sherhraza
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.

类别

Help CenterFile 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!

Translated by