Trying to create HTTP POST request in Lambda but Params are in gibberish - Java

45 Views Asked by At

I have the below code and the POST params are always gibberish even though I know my entry id is valid Getting the following message - Missing parameter &quot;entryId&quot;<

Seems like the entryid is &quot

Anyone know what I am doing wrong?


  // 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;
}```



Output 

```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 
0

There are 0 best solutions below