我試圖用Matlab webwrite發送以下POST請求:JSON有效載荷webwrite
POST https://url.to.com/hello/world
HEADERS {"API_KEY": "abc123"}
JSON PAYLOAD
{
"return_type": "hello",
"entities": ["ent1"],
"events": ["legal"],
"fields": [],
"filters": {},
"start_date": "2015-01-01 00:00:00",
"end_date": "2016-01-01 00:00:00",
"format": "csv",
"compressed": false
}
在Matlab中,我試過如下:
API_KEY = 'abc123';
url = 'https://url.to.com/hello/world';
options = weboptions(...
'MediaType', 'application/json', ...
'HeaderFields', {...
'API_KEY', API_KEY; ...
'Content-Type' 'application/json'});
payload.('return_type') = 'hello';
payload.('entities') = ['ent1'];
payload.('events') = ['legal'];
payload.('fields') = [];
payload.('filters') = {};
payload.('start_date') = '2015-01-01 00:00:00';
payload.('end_date') = '2016-01-01 00:00:00';
payload.('format') = 'csv';
payload.('compressed') = 'false';
response = webwrite(url, payload, options);
然而,這將返回錯誤:
The server returned the status 400 with message "Bad Request" in response to the request to URL
我試過上面的要求郵差者和它的工作。我也證實我的Matlab頭文件已經正確設置。所以它必須是我的JSON有效負載部分的Matlab設置。那裏有什麼問題?
更新1:
我注意到,當你運行jsonencode(payload)
它不會返回所需的格式。此外,"[ .. ]"
被剔除。我認爲問題從那時開始,因爲請求變得無效。所以我們需要一種方法來在必要時加入括號。