flimm
flimm

Reputation: 37

pass HTTP "_method" header to urlread2 function in matlab

Hello I try to use the urlread2 function within matlab to send a request to a REST API. In general this does work already, but for one specific API call there is a documented bug in the API: Normally I would use HTTP method "DELETE" and pass the request, like here:

headers  =     [http_createHeader('Content-Type','application/json; charset=UTF-8'),...
                http_createHeader('Accept','application/json; charset=UTF-8'),...
                http_createHeader('X-IG-API-KEY',api_key)];

json_body =  savejson('',body);

response = urlread2(myURL,'DELETE',json_body,headers);

This results in an error. Instead I am advised to use:

headers  =     [http_createHeader('Content-Type','application/json; charset=UTF-8'),...
                http_createHeader('Accept','application/json; charset=UTF-8'),...
                http_createHeader('X-IG-API-KEY',api_key,...
                http_createHeader('_method','DELETE')];

json_body =  savejson('',body);

response = urlread2(myURL,'POST',json_body,headers);

The reason for the bug is said to be that otherwise, for unknown reason, the message body gets lost during the process and that yields the error.

My problem is now that this workaround does work in the sandbox (no matlab code!). But using matlab as shown here doesn't change anything, same response in both cases. So I guess that the urlread2 function may not process the request as expected. Does anybody have an idea here?

The both dependencies here are wellknown matlab funcs:

JSONlab on matlabCentral urlread2 on matlabCentral

Upvotes: 1

Views: 470

Answers (1)

Jimbo
Jimbo

Reputation: 3284

urlread2, along with the original urlread, relies on underlying Java classes which do not support inclusion of a body with a delete request

See: HTTP Delete with Request Body issues

An alternative not mentioned on the page is to interface Matlab with Python's Requests library (requires 2014b or newer). I'm slowly working on this now, as it should allow more graceful handling of things like cookies as well as properly handle many bugs that the default Java implementation has (such as not including a body in a delete method)

Upvotes: 1

Related Questions