static string ToCurl(Microsoft.AspNetCore.Http.HttpContext httpContext, bool multiline, EnumFormat format) { // var url = Microsoft.AspNetCore.Http.Extensions.UriHelper.GetDisplayUrl(httpContext.Request); EnumQuoteType type = (format == EnumFormat.Cmd ? EnumQuoteType.DoubleQuote : EnumQuoteType.SimpleQuote); // https://curl.haxx.se/docs/ StringBuilder sb = new StringBuilder(); // command Helper.Append("curl", sb, multiline, format); // verb switch (httpContext.Request.Method) { case "GET": Helper.Append("-XGET", sb, multiline, format); break; } // header foreach (var item in httpContext.Request.Headers) { Helper.Append($"-H {item.Key}: {item.Value}", sb, type, format, multiline); } // url Helper.Append($"{httpContext.Request.Scheme}://{httpContext.Request.Host}{httpContext.Request.Path}{httpContext.Request.QueryString}", sb, multiline, format, true); var cmd = sb.ToString().Trim(); return(cmd); }
internal static void Append(string text, StringBuilder sb, EnumQuoteType type, EnumFormat format, bool multiLine, bool lastline = false) { if (multiLine && !lastline) { sb.AppendLine($@" {Helper.Quote(text, type)} {(format == EnumFormat.Cmd ? "^" : "\\")}"); } else { sb.Append(" " + Helper.Quote(text, type)); } }
static string Quote(string text, EnumQuoteType type) { if (type == CurlBuilder.EnumQuoteType.None) { return(text); } else if (type == CurlBuilder.EnumQuoteType.DoubleQuote) { return("\"" + text + "\""); } else if (type == CurlBuilder.EnumQuoteType.SimpleQuote) { return("\'" + text + "\'"); } else { return(text); } }