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
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
how can I make an unique number for invoice and ticketing system, something like
for tickets: ST-{yymm}-0000001
for Invoice: IN-{yymm}-0000001
Thank you
I recomend you create first a field that will be use to generate the code.
Once you have the field you have different options.
1- Assign that column in the database directly as autonumeric
2- Use a Logic Hook before_save that will be calculating the code in each save. For example:
<?php
//prevents directly accessing this file from a web browser
if(!defined('sugarEntry') ||!sugarEntry) die('Not A Valid Entry Point');
class CrearCodigoAutonumerico
{
public function CrearCodigoAutonumerico(&$bean, $event, $arguments)
{
// Si el campo está vacío...
if(empty($bean->codigo_c)) {
// Obtener el último código asignado
$db = DBManagerFactory::getInstance();
$query = "SELECT codigo_c
FROM <table_module>
ORDER BY codigo_c DESC LIMIT 1";
$result = $db->getOne($query, true);
$ultimo_codigo = $result;
// Calcular y asignar el nuevo código
$bean->codigo_c = $ultimo_codigo + 1;
}
}
}
Once you have this, you can create one more Logic Hook before save that constructs the name as you need it.
I am getting this req in my req.body . I want to parse the details .Below is my req.
"[{\"number\":\"INC0010075\",\"cmdb_ci\":\"hubot-test\",\"short_description\":\"test data for buisness rule 30\",\"category\":\"software\",\"comments\":\"\"}]"
I want output like
number:
cmdb_ci:
category:
How do i parse this array object in nodejs. Please help
Use JSON.parse() like this:
var aJsonArrString = "[{\"number\":\"INC0010075\",\"cmdb_ci\":\"hubot-test\",\"short_description\":\"test data for buisness rule 30\",\"category\":\"software\",\"comments\":\"\"}]"
var aObjList = JSON.parse(aJsonArrString);
for(var i = 0; i < aObjList.length; i++) {
console.log('number : ' + aObjList[i].number);
console.log('cmdb_ci : ' + aObjList[i].cmdb_ci);
console.log('category : ' + aObjList[i].category);
}
You Can Use
JSON.parse(req.body);
This looks like JSON, I don't know if the escaping \ are coming from your way of logging the value or something so I'll expect it is a valid string to start off.
You can use
var my_list = JSON.parse(req.body);
//Access like any other array...
my_list[0].number;
I have a JSON response in SoapUI that looks like this:
{
"civilite" : "1" ,
"nom" : "Fitz",
"prenom" : "Quinn",
"dateN" : "07/10/1953"
}
But I want to use JsonPath to retrieve only a part of this data, so I could have something like this:
{
"nom" : "Fitz",
"prenom" : "Quinn"
}
Is there a way to apply a JsonPath expression to retrieve this information?
For the first request step, add Script Assertion and use below script. The script extract the nom and prenom values and set them at test case level custom properties with given property names. With Script Assertion, an additional Groovy Script test step can be avoided.
Then in the next test step, use property expansion, so that those values will be replaced automatically with actual values by soapui.
Script Assertion:
//Check if the response is non empty or null
assert context.response
//Parse Json
def parsedJson = new groovy.json.JsonSlurper().parseText(context.response)
log.info "Nom: ${parsedJson.nom}"
log.info "Prenom: ${parsedJson.prenom}"
//assert if nom and prenom are not empty
assert parsedJson.nom, "nom is null or empty in the response"
assert parsedJson.prenom, "prenom is null or empty in the response"
//Set the retrieved values at test case level properties NOM, PRENOM
context.testCase.setPropertyValue('NOM', parsedJson.nom as String)
context.testCase.setPropertyValue('PRENOM', parsedJson.prenom as String)
Change 2nd step request content as below with Property Expansion
{
"nom" : "${#TestCase#NOM}",
"prenom" : "${#TestCase#PRENOM}"
}
Try this
input = {
"civilite" : "1" ,
"nom" : "Fitz",
"prenom" : "Quinn",
"dateN" : "07/10/1953"
}
output ={};
input.reduce(function (result, currentObject) {
output = {
nom: currentObject.nom,
prenom: currentObject.prenom
};
return output;
}, output);
There is my function :
private function responseJson($datas, $code)
{
$serializer = $this->container->get('jms_serializer');
$context = new SerializationContext();
$context->setSerializeNull(true);
$response = new Response($serializer->serialize($datas, 'json', $context->enableMaxDepthChecks()), $code);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
I use serializer components of symfony. This is what my server do when I call this function. The client need an array but the server returns an object. Is it possible to force to return an array ?
Thanks for your answers !
From what I read in the JsonSerializationVisitor the actual encoding is done with json_encode:
$result = #json_encode($this->getRoot(), $this->options);
As I can read in this post the forced creation of an array with json_encode cannot be done.
If your input data would be an array without indices, then json_encode would produce an array. That means that if key names in your data do matter, you cannot return an array to the client.
My problem is here I have a class in parse named PapaList where I have the column hijos that is and array of pointers, now I can get that column but when I try to navigate to the pointers in there those have a nombre column in another class like this:
PapaList->
|
objectId(string) -> papa(Pointer<_User>) -> hijos(Array[pointer])
In Alumnos class (where the pointers are "pointing to"):
Alumnos->
|
objectId(string)->nombre(string)*...
This is the column I want for each of the pointers * but i have not found a way to go there.
I have done the following:
func findAlumnos() {
if papa == PFUser.currentUser()! {
let queryHijos = PFQuery(className: "PapaList")
queryHijos.whereKey("papa", equalTo: papa)
queryHijos.includeKey("hijos")
print("papa = \(papa)")
do {
hijos = try queryHijos.findObjects()
let hijosIdx = "hijos"
let hijoOne = hijos[0][hijosIdx]
print("hijos = \(hijos[0][hijosIdx])")
print("primer hijo \(hijoOne)")
} catch let error {
print(error)
}
}
}
But this is not giving me a PFObject to index it with "nombre". It gives me what appears to be a string or an AnyObject.
primer hijo (
"<Alumnos: 0x7a7d81d0, objectId: nDapd6fIVc, localId: (null)> {\n apellido = Lopez;\n faltas = 5;\n fechaingreso = \"2015-09-18 06:34:00 +0000\";\n fechanac = \"2015-09-17 02:56:00 +0000\";\n grupoId = \"<Grupos: 0x7a7e3c80, objectId: 1kKmcDikef, localId: (null)>\";\n matricula = A006;\n nacionalidad = mexicana;\n nombre = Juanito;\n numlista = 2;\n}"
)
Any help here?? Thanks
By default, nested Parse objects are not returned with the top level query. If you have a class that has a single pointer to an object as a column, you can call use the include: method of PFQuery to also fetch that object. However, if you have an array of pointers, the only way to fetch the entire array is to pass that array into the whereKey:containedIn: method. In the iOS SDK, you can not pass an array of pointers, so you will actually have to iterate through your array of pointers, adding their objectIds to a separate array, then pass that array into whereKey:containedIn:.
http://parse.com/docs/ios/api/Classes/PFQuery.html
http://parse.com/docs/ios/api/Classes/PFQuery.html#//api/name/whereKey:containedIn: