Last active
February 15, 2025 09:43
-
-
Save gianlucaparadise/cb1a673129002fd4c77563a7658a1f15 to your computer and use it in GitHub Desktop.
Generate gRPC code snippets in Postman: put this code in the After Response of your Postman gRPC request to generate grpc_cli and grpcurl code snippets. It is very basic, but may be useful for many use cases.
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
/* cli code snippets logging */ | |
try { | |
const host = `${pm.request.url.host.join(".")}:${pm.request.url.port}`; | |
const grpc_cliServiceMethod = pm.request.methodPath.split('.').slice(1).join('.') | |
const grpcurlServiceMethod = pm.request.methodPath | |
const metadataEntries = []; | |
pm.request.metadata.each((header) => { | |
if (!header.disabled) { | |
metadataEntries.push(`"${header.key.toLowerCase()}:${header.value}"`); | |
} | |
}); | |
const grpc_cliMetadataString = metadataEntries.map((m) => `-metadata ${m}`).join(" \\\n "); | |
const grpcurlMetadataString = metadataEntries.map((m) => `-H ${m}`).join(" \\\n "); | |
const requestBody = pm.request.messages.idx(0).data; | |
const jsonBody = JSON.stringify(requestBody); | |
const grpc_cliCommand = `grpc_cli call --json_input --json_output ${host} ${grpc_cliServiceMethod} \\ | |
${grpc_cliMetadataString} \\ | |
'${jsonBody}'`; | |
console.log("grpc_cli code snippet:"); | |
console.log(pm.environment.replaceIn(grpc_cliCommand)); | |
const grpcurlCommand = `grpcurl -plaintext \\ | |
${grpcurlMetadataString} \\ | |
-d '${jsonBody}' \\ | |
${host} ${grpcurlServiceMethod}`; | |
console.log("grpcurl code snippet:"); | |
console.log(pm.environment.replaceIn(grpcurlCommand)); | |
} catch(exception) { | |
console.error("error while building cli code snippets:", exception); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment