Swift Siesta访问响应原始数据 [英] Swift Siesta access response raw data

查看:95
本文介绍了Swift Siesta访问响应原始数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的API中有一个返回PDF文件内容的方法.

I've in my API a method that return a content of PDF file.

如何在成功回调中访问响应的原始数据?

How can I access the raw data of response in success callback?

推荐答案

所有Siesta响应均以原始数据(以基金会类型Data的形式)开始,然后通过

All Siesta responses start out as raw data (in the form of the Foundation type Data), then run through the transformer pipeline.

默认转换器管道根据服务器发送的Content-type标头解析J​​SON,文本和图像.该列表不包括PDF,因此,如果您的服务器发送的内容类型为application/pdf(或任何不是JSON,文本或图像内容类型的内容),则响应仍为原始的Data.管道结束:

The default transformer pipeline parses JSON, text, and images based on the Content-type header sent by the server. That list doesn’t include PDF, so if your server is sending a content type of application/pdf (or anything that isn’t a JSON, text, or image content type), the response will still be raw Data at the end of the pipeline:

request.onSuccess { entity in
  guard let data = entity.content as? Data else {
    print("Huh, got mystery response:", entity.content)
    return
  }
  // do stuff with data
}

如果没有得到Data-如果上面的代码显示为呵呵"-那么管道中的某些内容将转换响应.您可以使用Siesta的详细日志记录来找出以下内容:

If you aren’t getting Data — if the code above says "huh" — then something in your pipeline is transforming the response. You can use Siesta’s verbose logging to figure out what:

Siesta.LogCategory.enabled = LogCategory.detailed

在日志输出中查找:

  • Added config,当某些东西向管道中添加变压器时记录下来,
  • 在相关请求之前的 Resulting configurationpipeline部分,其中显示了可能适用于响应的所有变压器,以及
  • Applied transformerResponse after pipeline,以了解如何转换实际的服务器响应.
  • Added config, which is logged when something adds a transformer to the pipeline,
  • the pipeline section of the Resulting configuration before the request in question, which shows all the transformers that may apply to the response, and
  • Applied transformer and Response after pipeline to see how the actual server response gets transformed.

这篇关于Swift Siesta访问响应原始数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆