How to get a JSON object back from a Jenkins build request - json

Similar to how http://localhost/jenkins/job/job_name/25/api/json would return a JSON object with the details of build 26, is there a way to get a similar object when first initiating the job, i.e., before you know what the build number is?
I noticed the output from a curl post request to the build url returns html that includes a build number; however, I would prefer not to have to parse this in favor of having a JSON object with the build number in it. Currently, I am using:
curl -v --data "param1=value&param2=value" \
http://localhost/jenkins/job/job_name/buildWithParameters
which initiates the job fine and outputs a bunch of html. Is there a way to start this job and receive a JSON object with the build number?

The nextBuildNumber may not be the correct build number in all cases. If you have triggered two different builds of the same Job, we don't know which one got triggered first. There is a race condition here. Checking the build queue may not give the correct build number either.

If you query http://localhost/jenkins/job/job_name/api/json you can fetch the nextBuildNumber field anytime that will give you the next build number.
When you trigger a build, you can rest assured the build will get exactly this number.

As soon as the build has been triggered, you can get its URL back from:
http://localhost/jenkins/job/job_name/api/json?tree=lastBuild[url]
This will return the running build if there is one, or the latest completed build otherwise. You can then add "/api/json" to that URL to get your build's JSON object.

In my scenario I needed a JSONP data type to go through. What I did is get the raw object of my particular job from Jenkins so that then I can manipulate it as necessary.
Request:
$.ajax({
url: "http://<jenkins server>/job/<job name>/api/json?jsonp=?",
dataType: 'jsonp',
success: success
});
Success call:
var success = function(json) {
console.log('Raw JSON object for this job:');
console.log(json);
}
Then, get the info you need, such as:
console.log(json.lastCompletedBuild.number);
console.log(json.lastBuild.url);

Related

Jenkins API xpath like functionality for JSON

I am trying to use the jenkins API to retrieve a list of running jobs buildURLs and this works with this the query
https://jenkins.server.com/computer/api/xml?tree=computer[executors[currentExecutable[url]]]&depth=1&xpath=//url&wrapper=buildUrls
By searching for all executors on given jenkins server and then grabbing the urls and wrapping them in a xml buildUrls object
(what I actually want is a count of total running jobs but I can filter in api then get a .size of this once client side)
However the application I am uses only accepts json, although I could convert to json I am wondering if there is a way I can have this return a json object which contains just the buildUrls. Currently if I change return to json the xpath modification is removed (since its xml only)
And I instead get returned the list of all executors and their status
I think my answer lies within Tree but i cant seem to get the result I want
To return a JSON object you can modify the URL as below
http://jenkins.server.com/computer/api/json?tree=computer[executors[currentExecutable[url]]]&depth=1&pretty=true
It will not be possible to get only the build urls you need to iterate through all the executables to get the total running jobs. The reason is that build urls alone cannot give you information where it is running. Hence, unfortunately we need to iterate through each executor.
The output you will get is something like below.
Another way of doing this same thing is parsing via jobs
http://jenkins.server.com/api/json?tree=jobs[name,url,lastBuild[building,timestamp]]&pretty=true
You can check for building parameter. But here too you will not be able to filter output from url diretly in jenkins. Iteration would be required through each and every job.

Azure Logic Apps - Verify content in JSON file

I have a logic app that runs as follows:
Step 1 = Recurrence (the schedule for the logic app to run)
Step 2 = HTTP (Performing a POST call on a custom API that returns a JSON file)
Step 3 = Create Blob (which uploads said document from Step 2 and uploads it to blob storage)
What I would like to do now is add an extra step between Step 2 and Step 3. After making the HTTP POST call, I would like to verify the content inside the JSON file that gets returned. If there is an error present in the JSON file, I want the Logic App to stop there.
Is there a particular step that I can use in Azure's Logic App to verify the data and have that step decide if it should continue on or not.
For this requirement, you can refer to my logic app below:
1. I initialize a variable named "resultFromHTTP" to simulate the json from your HTTP request(step 2). And I delete some character, so the "resultFromHTTP" is not a valid json format.
2. Then I initialize another variable and use the expression json(variables('resultFromHTTP')) in its value.
3. Run the logic app, it will fail and show error message like below screenshot. If the json is in valid format, it will run success.
===============================Update==============================
For your latest question, if the result json from HTTP request is not in valid json format and not a very long string, you can do it like this:
The contains(... expression is contains(variables('resultFromHTTP'), 'Data Not Found'). Then you can do what you want under "if true" or "if false".
If the result json from HTTP request is in valid json format, you can use "Parse JSON" action to parse it and get the specified field and then judge if it equals to "Data Not Found".

How to process Vue/Axios Json payload posted data on Yii2

It took me a while to understand this, being that it was a little obvious. I will answer myself, so other can benefit of the answer and ofcourse to see if there's a better way to do this. The problem was based on Axios/Yii2 but I guess this will apply equally to other frontend libraries/frameworks sending data to Yii2.
I needed to post data from a small form made on Vuejs, sending the request Axios to a Action/Controller on Yii2, so data is sent on a simple POST request and the post is getting to the controller, but I was not able to receive the data on the action, $_POST | $post arrives empty (checked using xdebug).
As much as I remember, this had something to do with security. But I already tried by disabling public $enableCsrfValidation, so that was not the problem.
public $enableCsrfValidation = false;
But no matter what, data was not being added to the request/post data inside Yii2.
The following Image, explains the problem you will find there:
The Axisos method that sends the post with test data.
The Yii2 Action stpoed at the place, I should be able to see data.
The capture of the xdebug variables and data for the request.
The capture of Chrome where you can check the payload is sent.
The answer is as I said "kind of obvious", but I could not see that, and I am sure some other devs will probably fall on this.
After searching like crazy and asking everyone, I tried sending the request by using Postman app, yup the best thing I know to test apis.
Dont forgue to add the xdebug cookie to be able to debug your PHP Endpoint.
There I found the first clue «the obvious part», I was not sending data as a form-data, Axios and other libraries, send the data as a raw (application/json) payload.
This means that Yii2 will no be able to find the data inside the post request, yes its there but Yii2 magic will not work, neither you will find this data inside $GLOBALS or in $_POST.
So reading the Yii2 documentation I found that inside request I can use a function that will help me recovering the Raw data, so to do this use the following line:
$raw_data = Yii::$app->request->getRawBody();
Now, that data gets to you as a simple, raw json string, so use the power of PHP to parse it to an object.
$object= json_decode($raw_data );
And finally use the data inside by calling the properties you look for, sent on the pay load:
Json Payload:
{
"msg":"This is my payload",
"id":"11"
}
To use it:
echo $object->{'msg'}; // prints: This is my payload
So that's the way to handle that, now I would like some other points of view to see if there's a better way or cleaner way to do this. Hope it helps.

Variable is only set by JSON Post Processor with Debug Sampler enabled

I definitely feel like I am missing something obvious, but here it goes anyway. I have a script that does the following per user in a thread group:
login
get a json list for that user
extract a part of the list from the response with a regex extractor into a variable called feed
start a for loop with feed_matchNr number of iterations
start a Counter
get the nth item from the list and use JSON Post Processor on the feed variable to get an id
make an additional http request with that id
continue loop
This all works as expected ... until I disable the Debug Sampler. Then the http request no longer "expands" the variable, instead the request has something like /api/path/${myid}/foo and fails. What black magic is the Debug Sampler doing and how can I do the same magic without it?
Your issue is the following:
First understand that Post Processor only runs IF the is a Sampler in its scope:
http://jmeter.apache.org/usermanual/component_reference.html#postprocessors
So in you plan, when you put a Debug Sampler , it runs. If you disable it, it only runs for post comment, so it has not initialized the variable for post comment.
Also is there a reason to put -1 in JSON Post Processor, this means you want to get all values:
http://jmeter.apache.org/usermanual/component_reference.html#JSON_Path_PostProcessor

How can I get DotNetNuke 6.2 Service Framework to modelbind json data

DotNetNuke Serviceframework is based on ASP.NET MVC 2, and therefore does not include json modelbinding out of the box.
I've tried a number of approaches:
MVC3 jsonvalueprovider
custom json model binder
custom value provider
The code to register these was called, however the methods on these objects themselves were not called.
Registering these is an interesting area in itself as in DotNetNuke, we don't have access to the global.asax file.
I've also attempted to deserialize the request input stream, in the the controller, but I get a nullreferenceexception there, and I get the correct data size, but all nulls!
Any ideas?!
Ok,
I have a workaround that is clean and functional.
I'm using a jquery plugin from here .This converts json into a standard forms form post for you.
using this with jquery & knockout looks like this:
$.ajax({
url: '<%= ModulePath %>Api/Register/Search',
type: 'POST',
data: $.toDictionary(ko.mapping.toJS($root),"",true),
success: function (data) { //do something });
Leaving the question open, in case anyone has any ideas to get json working directly.
You need to register a JSON value provider to get this to work. See http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx for details.
The best way to register the value provider is to do it in your route mapper. Be sure to guard the registration to ensure it occurs only once, as the route mapper is occasionally called more than once. If you are going to do this in a module deployed to servers you don't control you should probably inspect the contents of the factories collection to ensure no other service has already registered the value provider.
Services Framework in DNN 7 is based on WebAPI and natively supports JSON, so this hassle will go away soon.