fullstorydev / grpcurl

Like cURL, but for gRPC: Command-line tool for interacting with gRPC servers
MIT License
10.35k stars 497 forks source link

Create Process Failed to Run Issue #466

Closed aneesh-dev1 closed 3 weeks ago

aneesh-dev1 commented 3 weeks ago

I am creating an R script which takes a bearer token as an input and an input folder as an input and runs grpcurl command with same but I am getting an issue

Error in system(grpcurl_command, intern = TRUE) :

'CreateProcess' failed to run 'C:\Users\anekuma\path_to_grpcurl\grpcurl.exe -plaintext -H "Authorization: Bearer xyyyyxyysyyyydyy

Below is my code

library(jsonlite)

# Function to process JSON files using gRPC via grpcurl
process_json <- function(bearer_token, hostname, port, method, input_folder, output_folder) {

  # Ensure output directory exists
  if (!dir.exists(output_folder)) {
    dir.create(output_folder)
  }

  # Get list of JSON files in the input folder
  json_files <- list.files(input_folder, pattern = "\\.json$", full.names = TRUE)

  for (json_file in json_files) {

    # Read JSON content
    input_json <- fromJSON(json_file)

    # Convert JSON content to a string
    input_json_str <- toJSON(input_json, auto_unbox = TRUE)

    # Prepare the grpcurl command
    grpcurl_command <- sprintf(
      '%s -plaintext -H "Authorization: Bearer %s" -d \'%s\' %s:%s %s',
      grpc_path,bearer_token, input_json_str, hostname, port, method
    )

    # Execute the grpcurl command and capture the output
    grpc_response <- system(grpcurl_command, intern = TRUE)

    # Check if grpcurl command was successful
    if (!is.null(attr(grpc_response, "status")) && attr(grpc_response, "status") == 0) {
      # Parse the response JSON
      output_data <- fromJSON(paste(grpc_response, collapse = "\n"))

      # Determine output file name
      output_file <- file.path(output_folder, basename(json_file))

      # Write output to file
      write(toJSON(output_data, pretty = TRUE), output_file)

      cat("Processed:", json_file, "->", output_file, "\n")
    } else {
      cat("Failed to process:", json_file, "\n")
    }
  }
}

# Define input variables
grpc_path <- #Replace with path
bearer_token <-   # Replace with your bearer token
hostname <- "abcd"  # Replace with your hostname
port <- "20090"  
method <- "package/Method"  # Replace with your gRPC method
input_folder <- "R_Data/"  
output_folder <- "R_Data/output/"  

# Run the function
process_json(bearer_token, hostname, port, method, input_folder, output_folder)

Can Anyone Help
dragonsinth commented 3 weeks ago

Hi @aneesh-dev1 -- I suspect the problem is in the construction of your grpcurl_command and how it's being passed to system. I don't know anything about R, but ChatGPT suggested using the paste function to add arguments to a command line before passing to system. 🤷