Home > Software engineering >  Send with ruby request with array
Send with ruby request with array

Time:07-05

I have to make a request to an API, and one of the properties of the body is an Array (Directorios), my question is, how to send it as an array type, and not as a string as I show it in the following code

require 'uri'
require 'net/http'
require 'json'

UbicacionesCambios = Array.new
UbicacionesCambios.push("/Codigo/WebApi/Bccr.WebApi.Auten.csproj")
UbicacionesCambios.push("/Codigo/WebApi/Bccr.WebApi.Auten.csproj1")
#Actualizacion de codigo de nuget BCCR
temp = ""
puts "  Preparacion de actualizacion"
uri = URI('http://localhost/api/Devops.API/migrador?api-version=1')
req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
req.body = {
    "id":"9dedf35e-7df-4992-b03d-edfe24c302a9",
    "NombreProyecto":"WebApi",
    "NombreColeccion":"Insti",
    "AzureHostName":"http://mydomain:8080/tfs",
    "Rama":"test",
    "UltimoCommit":"f4afdfasdfasdfasdfadsfdsfdda2faf00c25a0a",
    "Componente":"WebApi.Nuget",
    "Version":"1.0.0.1",
    "Directorios": ["#{UbicacionesCambios}"]
    #"Directorios":["1","2"]
}.to_json
puts "  Ejecucion de actualizacion codigo nugets BCCR"
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
    http.request(req)
end
puts res.body if res.is_a?(Net::HTTPSuccess)
puts "  Resultado actualizacion de codigo #{res.body}"

Why when it reaches the Api, it is shown like this ["/Codigo/WebApi/Bccr.WebApi.Auten.csproj", "/Codigo/WebApi/Bccr.WebApi.Auten.csproj1"] an array of a single field

CodePudding user response:

You are forcing array to evaluate as string there. UbicacionesCambios is already an array You can just do it like this:

req.body = {
  # ...
  "Directorios": UbicacionesCambios 
}
  • Related