Unable to trigger Jenkins job using API Token command-line curl - jenkins

I'm new to this and not sure if I'm doing this right.
My requirement is to trigger(build) a jenkins job using command-line which I will invoke using ansible.
I'm following the instructions on this stackoverflow link.
I followed the below steps but Unfortunately the build does not get triggered.
Step 1:
I logged in Jenkins portal https://myjenkins.com:9043 using my user id user114 which is also the Jenkins administrator.
I then created API-token for my user id which is 118f32aa48601c136d29y11f3dd0e107f5.
Step 2:
I then selected Trigger Build Remotely option for job5 and gave the token name as 118f32aa48601c136d29y11f3dd0e107f5
Step 3: I then created Jenkins-Crumb using the below command:
`curl -s -k 'https://user114:118f32aa48601c136d29y11f3dd0e107f5#myjenkins.com:9043/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)'
Jenkins-Crumb:5196c183ad95cf3c0482873e34236f3b78ab628ecba968086cd19s7430016a4e`
Then i tried the below commands with the intention of triggering the build for job5 but none of them triggered my Jenkins build.
Attemp 1:
$ curl -I -k -X POST https://user114:118f32aa48601c136d29y11f3dd0e107f5#myjenkins.com:9043/job/job5/build -H "Jenkins-Crumb:5196c183ad95cf3c0482873e34236f3b78ab628ecba968086cd19s7430016a4e"
HTTP/1.1 400 Bad Request
X-Content-Type-Options: nosniff
Cache-Control: must-revalidate,no-cache,no-store
Content-Type: text/html;charset=iso-8859-1
Content-Length: 481
Server: Jetty(NOTHING)
Attempt 2:
$ curl -k -X POST https://user114:118f32aa48601c136d29y11f3dd0e107f5#myjenkins.com:9043/job/job5/build -H "Jenkins-Crumb:5196c183ad95cf3c0482873e34236f3b78ab628ecba968086cd19s7430016a4e"
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 400 Nothing is submitted</title>
</head>
<body><h2>HTTP ERROR 400 Nothing is submitted</h2>
<table>
<tr><th>URI:</th><td>/job/job5/build</td></tr>
<tr><th>STATUS:</th><td>400</td></tr>
<tr><th>MESSAGE:</th><td>Nothing is submitted</td></tr>
<tr><th>SERVLET:</th><td>Stapler</td></tr>
</table>
<hr>Powered by Jetty:// NOTHING<hr/>
</body>
Attempt 3:
$ curl -k -X POST https://user114:118f32aa48601c136d29y11f3dd0e107f5#myjenkins.com:9043/job/job5/build?token=118f32aa48601c136d29y11f3dd0e107f5 -H "Jenkins-Crumb:5196c183ad95cf3c0482873e34236f3b78ab628ecba968086cd19s7430016a4e"
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 400 This page expects a form submission</title>
</head>
<body><h2>HTTP ERROR 400 This page expects a form submission</h2>
<table>
<tr><th>URI:</th><td>/job/job5/build</td></tr>
<tr><th>STATUS:</th><td>400</td></tr>
<tr><th>MESSAGE:</th><td>This page expects a form submission</td></tr>
<tr><th>SERVLET:</th><td>Stapler</td></tr>
</table>
<hr>Powered by Jetty:// NOTHING<hr/>
</body>
</html>
Further, I also intend to pass parameters to this jenkins job which i dont know how-to.
Can you please suggest ?

I started using the Generic Webhook Plugin for this. It takes away a lot of the hassle and allows you to parse any JSON/XML payload as you please.

Related

How to POST with curl and not get HTTP error 422 'Unprocessable Entity'?

I am trying to send a POST request to an sign in endpoint. I keep getting HTTP error 422 'Unprocessable Entity'. How do I get past this error?
Here are some examples of commands I have tried:
curl -v -X POST -F 'user[email]=test#email.com' -F 'user[password]=password' https://example.com/users/sign_in
curl -v -H 'Accept: text/html,application/xhtml+xml,application/xml;' -H 'Content-Type: application/x-www-form-urlencoded' -d 'utf8=%E2%9C%93&authenticity_token=123456789123456789123456789123123123456456456789789789789VX4KgMBr6zgGjo123456789123456789njQ%3D%3D&user%5Bemail%5D=test%40sign_in.com&user%5Bpassword%5D=password&commit=Sign+in' https://example.com/users/sign_in
I want the request to send back a 200 or 301/302 response status, but instead I get this:
* upload completely sent off: 208 out of 208 bytes
< HTTP/1.1 422 Unprocessable Entity
< Server: nginx/1.12.2
< Date: Fri, 08 Feb 2019 12:08:07 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 0
< Connection: keep-alive
< X-Request-Id: 6ceb5baa-7740-448a-8105-6f67dd203fbb
< X-Runtime: 0.021676
Any help or advice is appreciated. Thanks
Update
I see now that the Rails log from such a request renders an InvalidAuthenticityToken Error
Started POST "/users/sign_in" for 127.0.0.1 at 2019-02-08 14:30:50 +0000
Processing by Users::SessionsController#create as */*
Can't verify CSRF token authenticity.
Completed 422 Unprocessable Entity in 1ms (ActiveRecord: 0.0ms)
ActionController::InvalidAuthenticityToken - ActionController::InvalidAuthenticityToken:
Any ideas on how to get past this? All I am seeking to do is ensure that routes which require authentication are returning 200/301/302 responses.
There are at least two ways to do this:
Option 1: get and use a token
One way to solve this is to actually capture and utilize the authenticity token that rails generates. To do this, we also need to capture and then send back cookies, to allow ourselves to utilize a session that is associated with the generated token. I was able to get this to work with something like the following incantation.
(Note: I'm assuming a scaffold has been created for "thing", creating a things_controller.rb, etc. Adapt this to your use case as needed.)
step 1: capture the token:
token=$(curl -s -c myjar https://example.com/things/new | \
sed -n 's/.*name="authenticity_token" value="\([^"]*\)".*/\1/p')
step 2: use it in the request:
cat myjar | \
curl -b - -s \
-H "Accept: application/json" \
-H "Content-type: application/json" \
-d '{"thing": {"field1":"Test","field2":"Testing"},
"authenticity_token":"'"$token"'"}' \
https://example.com/things | jq .
This works, without any changes to the application. When I run that, I see a pretty rendering of the JSON from a successful object creation as output.
(Ignore the | jq . if you just want the raw JSON data, perhaps to send it along to something else.)
However, there is another option. With a small tweak to the controller, one can achieve a bit easier curl usage (no cookiejar required, fewer parameters, only a single curl command, and no strange nested quotes), as follows:
Option 2: disable token checking:
Another way to get past this is to disable the validity_token checking for the create action in any controller(s) you wish to allow this sort of thing from. For example, given my example of a scaffold called "thing", one might update the app/controllers/things_controller.rb file to add the second line shown here:
class ThingsController < ApplicationController # EXISTING LINE
skip_before_action :verify_authenticity_token, only: [:create] # NEW LINE
before_action :set_img, only: [:show, :edit, :update, :destroy] # EXISTING LINE
# ... additional content elided ...
And then rails simply won't do that check, so one can do a one-step requset something like:
curl -s \
-H "Accept: application/json" \
-H "Content-type: application/json" \
-d '{"thing": {"field1":"Test","field2":"Testing"}}' \
http://example.com/things | jq .
Again, I see the jq-generated output, this time without having to capture cookies or a token or anything.

Jenkins REST API Create job

I'm creating a new job in Jenkins using the REST API. I tried the below curl command lines, but they are throwing an error
curl -i -X POST --user "admin:<API token>" --data-binary "#C:\mylocalconfig.xml" -H "Content-Type: text/xml" http://localhost:8080/createItem?name=NewJob
curl -X POST -u <username>:<pass> -H "Content-Type:application/xml" -d "#C:\mylocalconfig.xml" "http://localhost:8080/createItem?name=AA_TEST_JOB3"
Error:
HTTP/1.1 403 No valid crumb was included in the request
Date: Fri, 01 Jul 2016 05:25:59 GMT
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=ISO-8859-1
Cache-Control: must-revalidate,no-cache,no-store
Content-Length: 360
Server: Jetty(9.2.z-SNAPSHOT)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Error 403 No valid crumb was included in the request</title>
</head>
<body><h2>HTTP ERROR 403</h2>
<p>Problem accessing /createItem. Reason:
<pre> No valid crumb was included in the request</pre></p><hr><i><small>Power
ed by Jetty://</small></i><hr/>
</body>
</html>
Jenkins by default has CSRF Protection enabled which prevents one-click attacks. To invoke the request, you need to obtain the crumb from /crumbIssuer/api/xml using your credentials and include it into your request.
For example:
CRUMB=$(curl -s 'http://USER:TOKEN#localhost:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')
Then you can create a job (by including the crumb into your header):
curl -X POST -H "$CRUMB" "http://USER:TOKEN#localhost:8080/createItem?name=NewJob"
If the above won't work, check your crumb (echo $CRUMB) or run curl with -u USER:TOKEN.
For a more detailed explanation, see: Running jenkins jobs via command line.
If you are using Postman to fire the requests, using #kenorb example above, get the crumb
To create a folder at root level using the createItem endpoint
To create a sub-folder inside the folder created above using the createItem endpoint

Update a page in Confluence using REST API

This is what I've currently got and it creates a new Confluence page. It doesn't update it. Also it posts it in the root space, TST, but I want it to be in TST/space1/subsection2/updateThisPage.
curl -v -u admin:admin -X POST -H Content-Type: application/json -d "{\"id\":\"123456\",\"type\":\"page\",\"title\":\"new page\",\"space\":{\"key\":\"TST\",\"title\":\"updateThisPage\"},\"body\":{\"storage\":{\"value\":\"<p>This is the updated text for the new page</p>\",\"representation\":\"storage\"}},\"version\":{\"number\":3}}" http://localhost:8090/rest/api/content?spaceKey=TST&title=updateThisPage
This is the error message I get
{"statusCode":400,"message":"A page with this title already exists: A page already exists with the title new page in the space with key TST"}
Would it be a permissions error? I know I do not have access to delete.
Use request /rest/api/content/{id}.
This worked for me.
curl -u admin:admin -X PUT -H "Content-Type: application/json" -d "{\"id\":\"26738701\",\"type\":\"page\",\"title\":\"new page\",\"space\":{\"key\":\"RO\"},\"body\":{\"storage\":{\"value\":\"<p>UPDATE This is a new page</p>\",\"representation\":\"storage\"}},\"version\":{\"number\":2}}" http://localost:10080/rest/api/content/26738701
JSON Payload:
{
"id":"26738701",
"type":"page",
"title":"new page",
"space":{
"key":"RO"
},
"body":{
"storage":{
"value":"<p>UPDATE This is a new page</p>",
"representation":"storage"
}
},
"version":{
"number":2
}
}
Don't forget to use:
content ID in data part
version number in data part
PUT request
content ID in request
Try to use PUT instead of POST.
curl -v -u admin:admin -X PUT -H Content-Type: application/json -d "{\"id\":\"123456\",\"type\":\"page\",\"title\":\"new page\",\"space\":{\"key\":\"TST\",\"title\":\"updateThisPage\"},\"body\":{\"storage\":{\"value\":\"<p>This is the updated text for the new page</p>\",\"representation\":\"storage\"}},\"version\":{\"number\":3}}" http://localhost:8090/rest/api/content?spaceKey=TST&title=updateThisPage
If anyone is looking for javascript solution, here is my answer to another question like that
Unexpected grunt-http error when posting to Atlassian Confluence api
And here you can find working code i've developed on confluence hackathon
https://github.com/devex-web-frontend/dxWebPlugins/blob/master/src/confluence/helpers/buffer.js

Send post request

How i can send request like this via rails
POST /token/ HTTP/1.1
Host: api.admitad.com
Authorization: Basic XXX
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
grant_type=client_credentials&scope=public_data&client_id=XXX
based on admitad api
You can send the POST request via the command line with a tool like cURL:
curl --data-urlencode "grant_type=client_credentials&scope=public_data&client_id=XXX" http://api.admitad.com/token
If you want to use HTTP basic authorization, add the -u option:
curl --data-urlencode "grant_type=client_credentials&scope=public_data&client_id=XXX" http://api.admitad.com/token -u "client_id:public_key"
Another similar command line tool is httpie.org, which uses a slightly different syntax.

POST with curl according to a particular format

I'm attempting to gather XML data automatically using curl, and my command so far is
curl -E keyStore.pem -d 'uid=myusername&password=mypassword&active=y&type=F' 'https://www.aidaptest.naimes.faa.gov/aidap/XmlNotamServlet HTTP/1.1/' -k
but it keeps on giving me a "Your browser sent a query this server could not understand." error.
I'm pretty sure that it's connecting since it's not rejecting me, but I don't know how to properly format the POST. Here's some documentation they gave me for the format of the POST request.
POST <URL>/aidap/XmlNotamServlet HTTP/1.1
Content-type: application/x-www-form-urlencoded
Content-length: <input_parameter’s length>
<a blank line>
<input_parameter>`
input_parameter is uid, password, and location_id bit and is correct
Am I doing this correctly from what you can see?
Something like this should do it.
curl -E keyStore.pem -v -X POST -i --header Content-Type:application/x-www-form-urlencoded
-d 'uid=myusername&password=mypassword&active=y&type=F' 'https://www.aidaptest.naimes.faa.gov/aidap/XmlNotamServlet'
I don't think you need HTTP/1.1 at the end of the URL in the command line. And even if you needed it, you certainly don't need the final / character before the closing single quote.

Resources