Skip to content

Instantly share code, notes, and snippets.

@alejandro-isaza
Created November 19, 2013 19:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alejandro-isaza/7550829 to your computer and use it in GitHub Desktop.
Save alejandro-isaza/7550829 to your computer and use it in GitHub Desktop.
Convert a HttpURLConnection to a cURL command
public static String toCurlRequest(HttpURLConnection connection, byte[] body) {
StringBuilder builder = new StringBuilder("curl -v ");
// Method
builder.append("-X ").append(connection.getRequestMethod()).append(" \\\n ");
// Headers
for (Entry<String, List<String>> entry : connection.getRequestProperties().entrySet()) {
builder.append("-H \"").append(entry.getKey()).append(":");
for (String value : entry.getValue())
builder.append(" ").append(value);
builder.append("\" \\\n ");
}
// Body
if (body != null)
builder.append("-d '").append(new String(body)).append("' \\\n ");
// URL
builder.append("\"").append(connection.getURL()).append("\"");
return builder.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment