用于发送 HTTP 请求和接收 HTTP 响应的模块。
var {request} = require('ringo/httpclient');
var exchange = request({
method: 'GET',
url: 'http://ringojs.org/',
headers: {
'x-custom-header': 'foobar'
}
});
if(exchange.status == 200) {
console.log(exchange.content);
}
request({
url: "http://example.org/post-multipart",
method: "POST",
contentType: "multipart/form-data",
data: {
"image": new BinaryPart(binaryStream, "image.png"),
"document": new BinaryPart(binaryStream, "invoce.doc", "application/msword")
}
});
String | data | the data |
String | fileName | (optional) file name |
String | contentType | (optional) content type of the file |
BinaryPart | A newly constructed BinaryPart instance |
String | url | The URL |
Object | options | The options |
Exchange | A newly constructed Exchange instance |
此 Exchange 实例使用的连接。
响应体为 String。
响应正文为 ByteArray。
响应内容的长度。
响应内容类型。
由服务器设置的 Cookie。
响应编码。
响应标题。
响应状态消息。
响应状态码。
此 Exchange 实例包装的 URL。
request({
url: "http://example.org/post-multipart",
method: "POST",
contentType: "multipart/form-data",
data: {
"simple": new TextPart("my string", "utf-8"),
"text": new TextPart(textStream, "utf-8"),
"txtFile": new TextPart(txtStream, "utf-8", "test.txt")
}
});
String|TextStream | data | text data to write |
String | charset | the charset |
String | fileName | (optional) file name |
TextPart | A newly constructed TextPart instance |
执行 DELETE 请求。
String | url | The URL |
Object|String | data | The data to append as GET parameters to the URL |
Exchange | The Exchange instance representing the request and response |
执行 GET 请求。
String | url | The URL |
Object|String | data | The data to append as GET parameters to the URL |
Exchange | The Exchange instance representing the request and response |
执行 POST 请求。
String | url | The URL |
Object|String|Stream|Binary | data | The data to send to the server |
Exchange | The Exchange instance representing the request and response |
执行 PUT 请求。
String | url | The URL |
Object|String|Stream|Binary | data | The data send to the server |
Exchange | The Exchange instance representing the request and response |
提出一个通用的请求。
选项对象可能包含以下属性:
url
: 请求网址method
: 请求方法,如 GET 或 POSTdata
: 请求参数作为字符串或GET,DELETE等类似方法的对象。对于POST或PUT请求,主体必须是字符串,对象,流或二进制文件。对于多部分形式的POST请求,所有参数值必须是 TextPart
或
BinaryPart
的实例。headers
: 请求标题username
: 用于 HTTP 认证的用户名password
: 用于 HTTP 认证的密码proxy
: proxy-settings as string (http://proxy-hostname:port) or object {host: "proxy-hostname", port: 3128}
contentType
: contentType。如果设置为 multipart / form-data,则 PUT 和 POST 请求的数据将被视为多部分表单上传。binary
: 如果内容应作为二进制传送,则为 true,否则将解码为字符串followRedirects
: 是否应该自动跟踪 HTTP 重定向(响应代码 3xx);默认值:truereadTimeout
: 以毫秒为单位的读取超时设置。 0 返回意味着该选项被禁用(即,无限超时);默认值:30000 ms(或直到 impl 决定其时间)connectTimeout
: 设置一个指定的超时值,以毫秒为单位,用于打开与此 URLConnection 引用的资源的通信链接。零超时被解释为无限超时。默认值:60000 毫秒(或直到 impl 决定其时间)Object | options |
Exchange | exchange object |