How can I form this http request correctly in matlab

29 Ansichten (letzte 30 Tage)
Simon Parten
Simon Parten am 28 Nov. 2017
Kommentiert: Simon Parten am 26 Jan. 2018
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.
<option name="httpMethod" value="POST" />
<option name="urlBase" value="" />
<option name="urlPath" value="/atoken" />
<option name="headers">
<option name="key" value="Content-Type" />
<option name="value" value="application/x-www-form-urlencoded" />
<option name="key" value="Cache-Control" />
<option name="value" value="no-cache" />
<option name="parameters">
<option name="key" value="username" />
<option name="value" value="me" />
<option name="key" value="password" />
<option name="value" value="verySecret" />
<option name="key" value="grant_type" />
<option name="value" value="password" />
<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="" />
For the life of me, I cannot express it in matlab, I only ever get back 'Bad Request'. Can anyone help?
api = '';
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 Kommentar
Simon Parten
Simon Parten am 28 Nov. 2017
equally useless...
headerField ='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 =;
method =;
request =,headerField, aTest);
resp = send(request,uri, options);

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Sonam Gupta
Sonam Gupta am 7 Dez. 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 = '';
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 Kommentare
Simon Parten
Simon Parten am 7 Dez. 2017
Sadly, this doesn't work. I've had sucess with Excel VBA as follows.
Function getToken()
Dim xmlhttp As New MSXML2.XMLHTTP60
Dim myurl As String
Dim iCreds As iLogin
myurl = iStub & "aToken"
xmlhttp.Open "POST", myurl, False
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.setRequestHeader "Cache-Control", "no-cache"
ilmaCreds = readiLogin()
uname =
pword = iCreds.pword
xmlhttp.Send "grant_type=password&username=" & uname & "&password=" & pword
Set json = ParseJson(xmlhttp.responseText)
getToken = json("access_token")
End Function
When trying to reproduce this request in matlab, I have never managed to get any response other than 'Bad Request'. This makes me suspicious about the underlying mechanism and / or error message that is being reported.
Simon Parten
Simon Parten am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)


Mehr zu Call Web Services from MATLAB Using HTTP finden Sie in Help Center und File Exchange



Community Treasure Hunt

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

Start Hunting!

Translated by