如何在Dyalog APL的HttpCommand中POST原始数据?
使用HttpCommand提交纯文本POST请求的等效实现
对应你提供的cURL命令:
curl -request POST "http://my-api-url" --header "Content-Type: text/plain" --data-raw "here is my test"
等效的HttpCommand实现
可以通过两种方式实现:
分步配置方式
; 创建HttpCommand实例 hc ← ⎕NEW HttpCommand ; 设置请求地址 hc.URL ← 'http://my-api-url' ; 指定POST请求方法 hc.Method ← 'POST' ; 设置Content-Type头部 hc.Headers ← ⊂'Content-Type: text/plain' ; 传入纯文本数据 hc.Data ← 'here is my test' ; 执行请求并获取结果 result ← hc.Run
链式简洁写法
result ← ⎕NEW HttpCommand('POST' 'http://my-api-url' ('Content-Type: text/plain') 'here is my test').Run
关键说明
- 通过
Method属性指定请求方法为POST - 在
Headers中添加Content-Type: text/plain,保证服务端能正确解析纯文本数据 - 将纯文本内容直接赋值给
Data属性,这与cURL的--data-raw参数作用一致,HttpCommand会自动处理数据的传输格式
内容的提问来源于stack exchange,提问作者RikedyP
相关产品推荐
相关产品推荐

