How can I form this http request correctly in matlab

54 views (last 30 days)
This request, expressed in XML, works fine in a rest client.
I should clarify, that I work inside a corporate firewall. The request goes outside that.
<RestClientRequest>
<option name="httpMethod" value="POST" />
<option name="urlBase" value="https://aWebsite.net" />
<option name="urlPath" value="/atoken" />
<option name="headers">
<list>
<KeyValuePair>
<option name="key" value="Content-Type" />
<option name="value" value="application/x-www-form-urlencoded" />
</KeyValuePair>
<KeyValuePair>
<option name="key" value="Cache-Control" />
<option name="value" value="no-cache" />
</KeyValuePair>
</list>
</option>
<option name="parameters">
<list>
<KeyValuePair>
<option name="key" value="username" />
<option name="value" value="me" />
</KeyValuePair>
<KeyValuePair>
<option name="key" value="password" />
<option name="value" value="verySecret" />
</KeyValuePair>
<KeyValuePair>
<option name="key" value="grant_type" />
<option name="value" value="password" />
</KeyValuePair>
</list>
</option>
<option name="parametersEnabled" value="true" />
<option name="haveTextToSend" value="false" />
<option name="haveFileToSend" value="false" />
<option name="isFileUpload" value="false" />
<option name="textToSend" value="" />
<option name="filesToSend" value="" />
</RestClientRequest>
For the life of me, I cannot express it in matlab, I only ever get back 'Bad Request'. Can anyone help?
api = 'https://aWebsite.net/aToken';
body.username = 'me';
body.password = 'verySecret';
body.grant_type = 'password';
options = weboptions('MediaType', 'application/json' ,'RequestMethod','post','ArrayFormat','json');
respon = webwrite(api, body, options);
  1 Comment
Simon Parten
Simon Parten on 28 Nov 2017
equally useless...
headerField = matlab.net.http.field.ContentTypeField('application/x-www-form-urlencoded');
un = 'me';
pw = 'verySecret';
input = struct('Username',un,'Password',pw, 'grant_type', 'password');
inputParameters = struct('parameters', input);
aTest = jsonencode(inputParameters);
options = matlab.net.http.HTTPOptions();
method = matlab.net.http.RequestMethod.POST;
request = matlab.net.http.RequestMessage(method,headerField, aTest);
show(request)
resp = send(request,uri, options);

Sign in to comment.

Accepted Answer

Sonam Gupta
Sonam Gupta on 7 Dec 2017
According to my understanding, you want to read content from a RESTful webservice. When query parameters are to be appended to the URL, something like following should work:
url = 'https://aWebsite.net/aToken';
options = weboptions('RequestMethod', 'post', 'ArrayFormat','json');
data = webread(url,'username', 'me', 'password', 'verySecret', 'grant_type', 'password', options);
To encode the name-value pairs in the body of an HTTP POST request to the web service, you need to use webwrite. The web service defines response. Following example shows how you can use webwrite:
You can find more information and example about 'webread' and 'webwrite' at following links:
  2 Comments
Simon Parten
Simon Parten on 26 Jan 2018
There was a deeper problem to do with security. Once that was fixed, it was easy to form web requests as per the documentation.

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!

Translated by