request 静态方法

Future<HttpRequest> request(
  1. String url,
  2. {String? method,
  3. bool? withCredentials,
  4. String? responseType,
  5. String? mimeType,
  6. Map<String, String>? requestHeaders,
  7. dynamic sendData,
  8. void onProgress(
    1. ProgressEvent e
    )?}
)

创建并发送指定 url 的 URL 请求。

默认情况下,request 将执行 HTTP GET 请求,但也可通过指定 method 参数使用不同的方法(如 POSTPUTDELETE 等)。(有关只针对 POST 请求的详细信息,请参阅 HttpRequest.postFormData

当响应可用时,Future 完成执行。

如果指定,sendData 将以 ByteBufferBlobDocumentStringFormData 的形式与 HttpRequest 一起发送数据。

如果指定,responseType 设置请求所需的响应格式。默认值为 String,但也可以是 'arraybuffer'、'blob'、'document'、'json' 或 'text'。有关更多信息,请参阅 HttpRequest.responseType

指定 withCredentials 参数表示请求应指定诸如 cookie(已设置)或 授权头 之类的凭据。请注意在使用凭据时需要考虑的细节

  • 使用凭据仅在跨源请求中才有用。
  • urlAccess-Control-Allow-Origin 头不能包含通配符 (*)。
  • urlAccess-Control-Allow-Credentials 头必须设置为 true。
  • 如果没有将 Access-Control-Expose-Headers 设置为 true,则在调用 getAllResponseHeaders 时,将只返回所有响应头的一个子集。

以下与上面的 getString 示例等价

var name = Uri.encodeQueryComponent('John');
var id = Uri.encodeQueryComponent('42');
HttpRequest.request('users.json?name=$name&id=$id')
  .then((HttpRequest resp) {
    // Do something with the response.
});

以下是一个使用 FormData 提交整个表单的示例。

var myForm = querySelector('form#myForm');
var data = new FormData(myForm);
HttpRequest.request('/submit', method: 'POST', sendData: data)
  .then((HttpRequest resp) {
    // Do something with the response.
});

请注意,仅当 Chrome 扩展在其清单中具有适当的权限时,才能支持 file:// URI 的请求。对 file:// URI 的请求也永远不会失败 - Future 总是成功完成,即使在文件无法找到时也是如此。

另请参阅: 授权头

实现

static Future<HttpRequest> request(String url,
    {String? method,
    bool? withCredentials,
    String? responseType,
    String? mimeType,
    Map<String, String>? requestHeaders,
    sendData,
    void onProgress(ProgressEvent e)?}) {
  var completer = new Completer<HttpRequest>();

  var xhr = new HttpRequest();
  if (method == null) {
    method = 'GET';
  }
  xhr.open(method, url, async: true);

  if (withCredentials != null) {
    xhr.withCredentials = withCredentials;
  }

  if (responseType != null) {
    xhr.responseType = responseType;
  }

  if (mimeType != null) {
    xhr.overrideMimeType(mimeType);
  }

  if (requestHeaders != null) {
    requestHeaders.forEach((header, value) {
      xhr.setRequestHeader(header, value);
    });
  }

  if (onProgress != null) {
    xhr.onProgress.listen(onProgress);
  }

  xhr.onLoad.listen((e) {
    var status = xhr.status!;
    var accepted = status >= 200 && status < 300;
    var fileUri = status == 0; // file:// URIs have status of 0.
    var notModified = status == 304;
    // Redirect status is specified up to 307, but others have been used in
    // practice. Notably Google Drive uses 308 Resume Incomplete for
    // resumable uploads, and it's also been used as a redirect. The
    // redirect case will be handled by the browser before it gets to us,
    // so if we see it we should pass it through to the user.
    var unknownRedirect = status > 307 && status < 400;

    if (accepted || fileUri || notModified || unknownRedirect) {
      completer.complete(xhr);
    } else {
      completer.completeError(e);
    }
  });

  xhr.onError.listen(completer.completeError);

  if (sendData != null) {
    xhr.send(sendData);
  } else {
    xhr.send();
  }

  return completer.future;
}