web request with request params and content body

138 views Asked by At

I've used Matlab's webwrite() to make calls to a REST API, providing the requset params. However, I need to now make a call where the Request Body must be specified. Is there a way to do this?

The REST API is defined by a Java Spring controller, e.g.:

@PostMapping(value = "post")
public ResponseEntity<?> setMySTuff(
        @RequestParam(name = "myId") int myId,
        @RequestBody Collection<MyCustomObject> myObjList) {

THe data paramemter for webwrite seems intended for being a set of key/value request param pairs, and not a means of setting the request body.

1

There are 1 answers

0
Tommaso Belluzzo On

If I remember correctly, @RequestParam is used for mapping values as query parameters, while @RequestBody defines the content of the response. If my assumptions are valid, the Matlab equivalent should be:

url = ['http://mywebsite.net/service/?myId=' num2str(5778)];
body = struct('Item1','Hello','Item2','World');
opts = weboptions('MediaType','application/json','RequestMethod','post');

response = webwrite(url,body,opts);