模块 ringo / httpclient

用于发送 HTTP 请求和接收 HTTP 响应的模块。

Example

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);
}

Functions

Class BinaryPart

Class TextPart


BinaryPart (data, fileName, contentType)

Example

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")
  }
});

Parameters

String data

the data

String fileName

(optional) file name

String contentType

(optional) content type of the file

Returns

BinaryPart

A newly constructed BinaryPart instance


Exchange (url, options)

Parameters

String url

The URL

Object options

The options

Returns

Exchange

A newly constructed Exchange instance


Exchange.prototype. connection

此 Exchange 实例使用的连接。


Exchange.prototype. content

响应体为 String。


Exchange.prototype. contentBytes

响应正文为 ByteArray。


Exchange.prototype. contentLength

响应内容的长度。


Exchange.prototype. contentType

响应内容类型。


Exchange.prototype. cookies

由服务器设置的 Cookie。


Exchange.prototype. encoding

响应编码。


Exchange.prototype. headers

响应标题。


Exchange.prototype. message

响应状态消息。


Exchange.prototype. status

响应状态码。


Exchange.prototype. url

此 Exchange 实例包装的 URL。


TextPart (data, charset, fileName)

Example

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")
  }
});

Parameters

String|TextStream data

text data to write

String charset

the charset

String fileName

(optional) file name

Returns

TextPart

A newly constructed TextPart instance


del (url, data)

执行 DELETE 请求。

Parameters

String url

The URL

Object|String data

The data to append as GET parameters to the URL

Returns

Exchange

The Exchange instance representing the request and response


get (url, data)

执行 GET 请求。

Parameters

String url

The URL

Object|String data

The data to append as GET parameters to the URL

Returns

Exchange

The Exchange instance representing the request and response


post (url, data)

执行 POST 请求。

Parameters

String url

The URL

Object|String|Stream|Binary data

The data to send to the server

Returns

Exchange

The Exchange instance representing the request and response


put (url, data)

执行 PUT 请求。

Parameters

String url

The URL

Object|String|Stream|Binary data

The data send to the server

Returns

Exchange

The Exchange instance representing the request and response


request (options)

提出一个通用的请求。

通用请求选项

选项对象可能包含以下属性:

  • url: 请求网址
  • method: 请求方法,如 GET 或 POST
  • data: 请求参数作为字符串或GET,DELETE等类似方法的对象。对于POST或PUT请求,主体必须是字符串,对象,流或二进制文件。对于多部分形式的POST请求,所有参数值必须是 TextPartBinaryPart 的实例。
  • 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);默认值:true
  • readTimeout: 以毫秒为单位的读取超时设置。 0 返回意味着该选项被禁用(即,无限超时);默认值:30000 ms(或直到 impl 决定其时间)
  • connectTimeout: 设置一个指定的超时值,以毫秒为单位,用于打开与此 URLConnection 引用的资源的通信链接。零超时被解释为无限超时。默认值:60000 毫秒(或直到 impl 决定其时间)

Parameters

Object options

Returns

Exchange

exchange object