You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Lambda中Java HTTP POST请求参数异常:缺失entryId问题求助

问题:AWS Lambda中Java编写的HTTP POST请求参数乱码,提示entryId缺失

已知entryId参数值有效,但发送POST请求后始终收到错误提示:Missing parameter "entryId",且entryId参数显示为"。相关代码及日志如下:

相关代码

// Open a connection to the URL
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// Set the request method to POST
connection.setRequestMethod("POST");

// Set the request headers
connection.setRequestProperty("Content-Type", "application/json");

// Enable input and output streams
connection.setDoOutput(true);

// Construct the POST data using parameters from the SNS message
String postData = "ks=" + ks + "&entryId=" + entry + "&baseEntry[tags]=" + cleanTag + "&baseEntry[objectType]=KalturaBaseEntry";

// Write the POST data to the connection
try (OutputStream os = connection.getOutputStream()) {
  byte[] postDataBytes = postData.getBytes(StandardCharsets.UTF_8);
  System.out.println("postDataBytes : "+postDataBytes );
  os.write(postDataBytes);
}

// Get the HTTP response
int responseCode = connection.getResponseCode();
System.out.println("HTTP Response Code: " + responseCode);

// Read the response content
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
  String inputLine;
  StringBuilder response = new StringBuilder();
  while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
  }
  System.out.println("HTTP Response Content: " + response.toString());
}

// Close the connection
connection.disconnect();
return true;
} catch (Exception e) {
  e.printStackTrace();
  return false;
}

日志输出

2023-10-31T16:46:24.935-04:00    entry : 1_06n6re6z  Link 

2023-10-31T16:46:24.935-04:00   tag : Happy, Sad, Smile Link 

2023-10-31T16:46:24.935-04:00   ks : djJ8NTU5OTLink 

2023-10-31T16:46:25.875-04:00   postDataBytes : [B@312ab28e Link 

2023-10-31T16:46:25.915-04:00   HTTP Response Code: 200 

问题原因及修复方案

核心问题

你设置的请求头Content-Type为application/json,但实际发送的是**表单URL编码(form-urlencoded)**格式的参数串,服务端无法正确解析参数,导致entryId识别异常。

修复步骤

  1. 修正Content-Type请求头
    将Content-Type改为表单编码格式:

    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    
  2. 对参数进行URL编码
    为避免特殊字符(比如空格、逗号等)导致参数解析错误,需要对每个参数值进行URL编码:

    String encodedKs = URLEncoder.encode(ks, StandardCharsets.UTF_8.name());
    String encodedEntry = URLEncoder.encode(entry, StandardCharsets.UTF_8.name());
    String encodedCleanTag = URLEncoder.encode(cleanTag, StandardCharsets.UTF_8.name());
    String postData = "ks=" + encodedKs + "&entryId=" + encodedEntry + "&baseEntry[tags]=" + encodedCleanTag + "&baseEntry[objectType]=KalturaBaseEntry";
    
  3. 优化日志排查
    当前日志打印postDataBytes输出的是字节数组的对象地址,无法看到实际参数内容,建议改为打印postData字符串:

    System.out.println("postData : " + postData);
    

内容的提问来源于stack exchange,提问作者user1163234

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 05:25:19