从REST API获取application/octet-stream格式tar文件无法解压排查
问题分析与解决方法
首先,你的情况大概率是API返回的内容虽然标了application/octet-stream,但实际是Base64编码后的字符串,而非原始二进制数据。你直接保存RawBytes的话,其实存的是Base64字符串本身的字节,自然没法被归档工具识别。
验证方式
把response.RawBytes转成字符串,看看是否和response.Content完全一致。如果一致,就实锤API把二进制tar文件做了Base64编码后返回,只是Content-Type头没对应上。
解决代码
用RestSharp处理的话,需要先把Base64字符串解码回原始二进制,再保存文件:
// 方法1:直接用response.Content解码 var base64Str = response.Content.Trim(); // 去除可能的首尾空白 var rawTarBytes = Convert.FromBase64String(base64Str); File.WriteAllBytes("output.tar", rawTarBytes); // 方法2:若RawBytes是Base64字符串的字节,先转成string再解码 var base64StrFromBytes = Encoding.UTF8.GetString(response.RawBytes); var rawTarBytes = Convert.FromBase64String(base64StrFromBytes); File.WriteAllBytes("output.tar", rawTarBytes);
结论
- 你的代码本身没有错误,但缺少了对API返回的Base64编码内容的解码步骤。
- API存在不规范问题:明明声明返回二进制流,却返回了Base64编码的文本,导致Content-Type头与实际返回内容不匹配。
内容的提问来源于stack exchange,提问作者Roli
相关产品推荐
相关产品推荐

