服务端生成小程序码的接口提示"412 Precondition Failed on POST request for "https://api.weixin.qq.com/wxa/getwxacodeunlimit%22: [no body]";
的实际请求的代码如下:
Object accessToken = redisService.getValue("weixin_access_token");
if (Objects.isNull(accessToken)) accessToken = getAccessToken();
// 请求 URL,注入 access_token
String url = String.format("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s", accessToken);
// 构造参数体
Map<String, Object> bodyMap = new HashMap<>();
bodyMap.put("scene", scene);
bodyMap.put("page", page);
bodyMap.put("width", 300);
// 正式版为 "release",体验版为 "trial",开发版为 "develop"。默认是正式版。
bodyMap.put("env_version", "release");
// 构造请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON); // 设置为 JSON 请求
// 使用 HttpEntity 封装请求体和请求头
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(bodyMap, headers);
// 发送 POST 请求获取二维码
ResponseEntity<byte[]> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, byte[].class);
return response.getBody();