Created
November 19, 2013 19:13
-
-
Save alejandro-isaza/7550829 to your computer and use it in GitHub Desktop.
Convert a HttpURLConnection to a cURL command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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