在Matlab中的curl命令

时间:2017-02-13 09:17:32

标签: matlab curl

我正在尝试复制一个在unix机器上工作正常的curl命令:

curl -X POST --insecure <ENDPOINT> -H "Content-Type: application/json" -H "<OTHER HEADERS>" -d @<PATH TO JSON FILE>

我可以使用什么Matlab命令在Windows机器上复制此命令?我很难找到添加--insecure选项的方法

非常感谢

1 个答案:

答案 0 :(得分:0)

让我们使用一个示例,我使用resclient进行emacs,但不要对此感到害怕

我的目标是使用matlab进行此调用:

#
# Request
#
:auth-token = abcd1234
:number := (+ 1 2 3 4)
:text := (concat "This is " ":num" "ber")
#
# Multiline variable referencing another variable
#
:common-headers = <<
Authentication: :auth-token
User-Agent: MyApp/1.0
Content-type: application/json
#
# ...and another one
:common-body = <<
{ "number": :number, "text": ":text" }
#
# Now, use them both in request
#
POST http://httpbin.org/post?q=1
:common-headers

:common-body

结果是:

{
  "args": {
    "q": "1"
  },
  "data": "{ \"number\": 10, \"text\": \"This is 10\" }",
  "files": null,
  "form": null,
  "headers": {
    "Accept": "*/*",
    "Accept-Charset": "utf-8;q=1, gb2312;q=0.5, iso-8859-1;q=0.5, big5;q=0.5, iso-2022-jp;q=0.5, shift_jis;q=0.5, euc-tw;q=0.5, euc-jp;q=0.5, euc-jis-2004;q=0.5, euc-kr;q=0.5, us-ascii;q=0.5, utf-7;q=0.5, hz-gb-2312;q=0.5, big5-hkscs;q=0.5, gbk;q=0.5, gb18030;q=0.5, iso-8859-5;q=0.5, koi8-r;q=0.5, koi8-u;q=0.5, cp866;q=0.5, koi8-t;q=0.5, windows-1251;q=0.5, cp855;q=0.5, iso-8859-2;q=0.5, iso-8859-3;q=0.5, iso-8859-4;q=0.5, iso-8859-9;q=0.5, iso-8859-10;q=0.5, iso-8859-13;q=0.5, iso-8859-14;q=0.5, iso-8859-15;q=0.5, windows-1250;q=0.5, windows-1252;q=0.5, windows-1254;q=0.5, windows-1257;q=0.5, cp775;q=0.5, cp850;q=0.5, cp852;q=0.5, cp857;q=0.5, cp858;q=0.5, cp860;q=0.5, cp861;q=0.5, cp863;q=0.5, cp865;q=0.5, cp437;q=0.5, macintosh;q=0.5, next;q=0.5, hp-roman8;q=0.5, adobe-standard-encoding;q=0.5, iso-8859-16;q=0.5, iso-8859-7;q=0.5, windows-1253;q=0.5, cp737;q=0.5, cp851;q=0.5, cp869;q=0.5, iso-8859-8;q=0.5, windows-1255;q=0.5, cp862;q=0.5, iso-2022-jp-2004;q=0.5, cp874;q=0.5, iso-8859-11;q=0.5, viscii;q=0.5, windows-1258;q=0.5, iso-8859-6;q=0.5, windows-1256;q=0.5, iso-2022-cn;q=0.5, iso-2022-cn-ext;q=0.5, iso-2022-jp-2;q=0.5, iso-2022-kr;q=0.5, utf-16le;q=0.5, utf-16be;q=0.5, utf-16;q=0.5, x-ctext;q=0.5",
    "Authentication": "abcd1234",
    "Content-Length": "38",
    "Content-Type": "application/json",
    "Extension": "Security/Digest Security/SSL",
    "Host": "httpbin.org",
    "Mime-Version": "1.0",
    "User-Agent": "MyApp/1.0"
  },
  "json": {
    "number": 10,
    "text": "This is 10"
  },
  "origin": "46.222.44.201",
  "url": "http://httpbin.org/post?q=1"
}
// POST http://httpbin.org/post?q=1
// HTTP/1.1 200 OK
// Server: nginx
// Date: Mon, 13 Feb 2017 10:29:40 GMT
// Content-Type: application/json
// Content-Length: 1768
// Connection: keep-alive
// Access-Control-Allow-Origin: *
// Access-Control-Allow-Credentials: true
// Request duration: 0.613051s

让我们把它变成卷曲:

curl -i -H 'Content-type: application/json' -H 'User-Agent: MyApp/1.0' -H 'Authentication: abcd1234' -XPOST 'http://httpbin.org/post?q=1' -d '{ "number": 10, "text": "This is 10" }'

最后将它翻译成matlab我建议你使用urlread2这是一个来自matlab fileexchange的matlab程序你只需要注册就可以下载它。它是由Jim Hokason制作的,对于最近的matlab版本(2016),你可以try this

来自here

的urlread2

上述请求可能是这样的:

>> %% Create the headers
>> hct = http_createHeader('Content-Type','application/json');
>> hua = http_createHeader('User-Agent','matlab');
>> ha = http_createHeader('Authentication','abcd1234');
>> %% method
>> method = 'POST';

>> body = '{"number":10, "text: "this is 10"}';

>> x = urlread2('http://httpbin.org/post?q=', method, body, [hct hua ha])

x =

{
  "args": {
    "q": ""
  }, 
  "data": "{\"number\":10, \"text: \"this is 10\"}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2", 
    "Authentication": "abcd1234", 
    "Content-Length": "34", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "matlab"
  }, 
  "json": null, 
  "origin": "46.222.44.201", 
  "url": "http://httpbin.org/post?q="
}

对于不安全的选项,我认为这不会验证ssl certifiactes