toshiomagic
toshiomagic

Reputation: 1649

How to set the body of a http post request in Matlab

I'm trying to post to a restful service with Matlab. I've tried using webread, webwrite, and urlread and I cannot figure out how to set the body of the message.

My body is json and looks like this:

{"Item1": "string1", "Item2": "string2"}

Upvotes: 3

Views: 3764

Answers (1)

toshiomagic
toshiomagic

Reputation: 1649

I found out what I was doing wrong. I was constructing my body as a string literal and not as a matlab struct. Correct way:

api = 'http://myurl.net';
url = [api, '/Login'];
[un, pw] = GetAuthentication;
input = struct('Username',un,'Password',pw);
opts = weboptions('MediaType','application/json');
userInfo = webwrite(url, input, opts);

Upvotes: 5

Related Questions