3

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"}

7
  • You're trying to post to the service, so have you tried webwrite instead of webread? Commented Sep 21, 2015 at 2:55
  • Yes I have. Sorry for not listing that. I also tried webread with post in the weboptions object. Commented Sep 21, 2015 at 12:47
  • Have you tried passing the item names and values separately as outlined in the first paragraph of the documentation? mathworks.com/help/matlab/ref/webwrite.html Commented Sep 21, 2015 at 17:55
  • Yes, it returns a 500 error :/ Commented Sep 23, 2015 at 2:33
  • I'm using postman to log in and it works fine. But, because I am working from a vpn, I cannot use fiddler to intercept my web traffic and analyze it. Commented Sep 23, 2015 at 2:38

1 Answer 1

5

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); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.