Skip to content

下载文件 ​

根据文件消息回调中的 XML 数据下载文件,并返回文件访问地址或文件名。

接口地址: POST /getMsgFile

前置条件 ​

  • 当前 wId 对应的微信实例在线。
  • msgId、content 来自文件消息回调,不建议手动拼接。
  • 文件消息的 content 中需要包含 appattach、cdnattachurl、aeskey 等信息。

请求参数 ​

参数名必选类型说明
wId是string登录实例标识
msgId是number消息 ID
content是string文件消息回调中的 XML 数据

请求示例 ​

http
POST {BASE_URL}/getMsgFile
Authorization: Bearer <你的调用凭证>
Content-Type: application/json

{
  "wId": "{{wId}}",
  "msgId": 1102684150,
  "content": "<?xml version=\"1.0\"?><msg><appmsg><title>下载文件.txt</title><type>6</type><appattach><totallen>6</totallen><attachid>@cdn_...</attachid><fileext>txt</fileext><cdnattachurl>304e020100...</cdnattachurl><aeskey>17dd9d048f84c77db909b2161d6dbb09</aeskey></appattach></appmsg></msg>"
}
bash
curl -X POST '{BASE_URL}/getMsgFile' \
  -H 'Authorization: Bearer <你的调用凭证>' \
  -H 'Content-Type: application/json' \
  --data-raw '{
  "wId": "{{wId}}",
  "msgId": 1102684150,
  "content": "<?xml version=\"1.0\"?><msg><appmsg><title>下载文件.txt</title><type>6</type><appattach><totallen>6</totallen><attachid>@cdn_...</attachid><fileext>txt</fileext><cdnattachurl>304e020100...</cdnattachurl><aeskey>17dd9d048f84c77db909b2161d6dbb09</aeskey></appattach></appmsg></msg>"
}'
python
import requests

url = "{BASE_URL}/getMsgFile"
headers = {"Authorization": "Bearer <你的调用凭证>", "Content-Type": "application/json"}
body = r"""{
  "wId": "{{wId}}",
  "msgId": 1102684150,
  "content": "<?xml version=\"1.0\"?><msg><appmsg><title>下载文件.txt</title><type>6</type><appattach><totallen>6</totallen><attachid>@cdn_...</attachid><fileext>txt</fileext><cdnattachurl>304e020100...</cdnattachurl><aeskey>17dd9d048f84c77db909b2161d6dbb09</aeskey></appattach></appmsg></msg>"
}"""

resp = requests.post(url, headers=headers, data=body.encode("utf-8"))
print(resp.text)
java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

String url = "{BASE_URL}/getMsgFile";
String body = "{\n" +
    "  \"wId\": \"{{wId}}\",\n" +
    "  \"msgId\": 1102684150,\n" +
    "  \"content\": \"<?xml version=\\\"1.0\\\"?><msg><appmsg><title>下载文件.txt</title><type>6</type><appattach><totallen>6</totallen><attachid>@cdn_...</attachid><fileext>txt</fileext><cdnattachurl>304e020100...</cdnattachurl><aeskey>17dd9d048f84c77db909b2161d6dbb09</aeskey></appattach></appmsg></msg>\"\n" +
    "}";

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
    .header("Authorization", "Bearer <你的调用凭证>")
    .header("Content-Type", "application/json")
    .POST(HttpRequest.BodyPublishers.ofString(body))
    .build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
php
<?php

$url = '{BASE_URL}/getMsgFile';
$body = <<<'JSON'
{
  "wId": "{{wId}}",
  "msgId": 1102684150,
  "content": "<?xml version=\"1.0\"?><msg><appmsg><title>下载文件.txt</title><type>6</type><appattach><totallen>6</totallen><attachid>@cdn_...</attachid><fileext>txt</fileext><cdnattachurl>304e020100...</cdnattachurl><aeskey>17dd9d048f84c77db909b2161d6dbb09</aeskey></appattach></appmsg></msg>"
}
JSON;

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer <你的调用凭证>',
    'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

响应参数 ​

参数名类型说明
codestring1000 成功;其他值都是失败,通常是 1001
messagestring反馈信息
dataobject下载结果
data.urlstring文件地址或文件名,按接口返回值使用

成功响应 ​

json
{
  "code": "1000",
  "message": "成功",
  "data": {
    "url": "下载文件.txt"
  }
}

错误响应 ​

json
{
  "code": "1001",
  "message": "失败",
  "data": null
}

使用建议 ​

  • 文件下载对 XML 完整性要求较高,建议直接保存并传入回调原始 content。
  • 业务侧展示文件时,可优先读取 XML 中的 title 和 fileext。
  • 如果返回的是临时地址,建议及时转存。