Swift Siesta access response raw data

帅比萌擦擦* 提交于 2019-12-02 19:59:25

问题


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?


回答1:


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

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
}

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

Look in the log output for:

  • 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.


来源:https://stackoverflow.com/questions/46307224/swift-siesta-access-response-raw-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!