Convert har to curl (Generator)

Hi everyone ! I hope you are fine today !

Because we had issue to share fastly a webservice (in error) across the team we decided to use a curl generator.
In that way, with a fast copy past, we can replay the API used or at least set up the basic request data like header/Auth/body/…

See groovy repo for the class at harToCurl/harToCurl.groovy at master · SolannP/harToCurl · GitHub

It’s utterly based on another repository available at GitHub - snoe/harToCurl: Convert webdev HTTP Archive Request files (.har) to curl requests, the only work have been to translate code from python to groovy

package test.common
import com.kms.katalon.core.configuration.RunConfiguration

import groovy.json.JsonSlurper
import groovy.transform.PackageScope

/**
 * The class allow you to convert har string to a curl command
 * It's utterly based on another repository available at https://github.com/snoe/harToCurl
 * The only work there have been to translate code from python to groovy
 * See groovy repo at https://github.com/SolannP/harToCurl
 */
public class HarToCurl {

	/**
	 * Convert a har string to a curl command
	 * Be aware that only the first log file will be convert, 
	 * @param har as Sting
	 * @return curl
	 */
	public static String convert(String harAsString) {
		def request = new JsonSlurper().parseText( harAsString ).log.entries.request
		def method = request.method[0]
		def url = request.url[0]
		def cookies = formatCookies(request.cookies[0])
		def http = formatHttp(request.httpVersion[0])
		def headers = formatHeader(request.headers[0])
		def postData = formatPostData(request.postData.text)

		return "curl -X ${method} ${http} -b \'${cookies}\' ${headers} -d \'${postData}\' '${url}'"
	}

	@PackageScope
	def static formatCookies(def cookies){
		def cookiesForCurl
		def urlEncodeMap = { aMap ->
			// e.g. given map [x:1, y2] returns x=1&y=2
			def encode = { URLEncoder.encode( "$it".toString(), "UTF-8") }
			return aMap.collect { encode(it.key) + '=' + encode(it.value) }.join('&')
		}
		if(cookies) cookiesForCurl = urlEncodeMap(cookies);
		else cookiesForCurl = ""

		return cookiesForCurl;
	}

	@PackageScope
	def static formatHttp(def httpVersion) {
		def AllHttpVersions = [
			('HTTP/1.0'): '--http1.0',
			('HTTP/1.1'): '--http1.1',
			('HTTP/2.0'): '--http2',
			(''):''
		]
		return AllHttpVersions.get(httpVersion.toString().toUpperCase())
	}

	@PackageScope
	def static formatHeader(def header) {
		def headers = ""
		header.each { headers += " -H \"${it.name}\":\"${it.value}\""}
		return headers
	}

	@PackageScope
	def static formatPostData(def postData) {
		def bodyOfWebService = ""
		if(postData)  bodyOfWebService = postData[0]
		else bodyOfWebService = ""
		return bodyOfWebService
	}
}

The class can be call using HarToCurl.convert(harAsString)

See below an code sample if you need to generate har (some modification might be necessary to fit your needs)

import org.apache.commons.io.FileUtils
import com.kms.katalon.core.configuration.RunConfiguration
// import your HarToCurl class

def reportDirectory = RunConfiguration.getReportFolder()+"/requests/main/"
def curlCommande = ""
File source = new File(reportDirectory);
if(source.isDirectory()) {
	File[] content = source.listFiles();
	for(int i = 0; i < content.length; i++) {
		curlCommande = HarToCurl.convert(content[i].text)
		File fileToStore = new File( reportDirectory + content[i].getName()+"_curlx.txt");
		FileUtils.writeStringToFile(fileToStore, curlCommande , "UTF-8");
	}
}
else println(RunConfiguration.getReportFolder()+" NOT A DIR !!")

Feel free to add a little :hearts::wink:

Take care !

4 Likes