Attempts to use curl in conjunction with api give 404 error - python

I am attempting to query Confluence knowledge base pages using the API and failing.
I have researched the api and have the following:
Browsing Content
curl -u admin:admin http://localhost:8080/confluence/rest/api/content/ | python -mjson.tool
Read Content and Expand the Body:
curl -u admin:admin http://localhost:8080/confluence/rest/api/content/3965072?expand=body.storage | python -mjson.tool
What I actually want to do is dump out the contents of a page / pages in a "Space" as Confluence calls it to a file, or on screen.
This is an actual page:
"https://bob.atlassian.net/wiki/spaces/BKB/pages/1234567/BOB+KNOWLEDGE+BANK"
And this is an example I found of using the REST API to do what I want:
curl -u admin:admin -G "http://myinstance.address:8090/rest/api/content/search" --data-urlencode "cql=(type=page and space=low and title ~ "LOWERTOWN")" \ | python -m json.tool
How I am translating what I have researched:
curl -u "bob#bob.com:12345678" -G "https://bob.atlassian.net/rest/api/content/search" --data-urlencode "cql=(type=page and space=BKB and title ~ "BOB+KNOWLEDGE+BANK")" \ | python -m json.tool
Which results in this error:
curl: (3) URL using bad/illegal format or missing URL
No JSON object could be decoded
I have grabbed my logic here from this site:
https://community.atlassian.com/t5/Confluence-questions/How-to-get-Conflunece-knowledge-base-data-through-API/qaq-p/1030159
And I am assuming I have misunderstood this:
/rest/api/content/search
And where it belongs in my curl statement and linking it to the knowledge base. I am also unsure if applying the -mjson.tool is applicable in my case or if I actually have it installed / need to verify that.
Can you help me interpret this correctly?

You are almost there! You just need to pass cql as query parameter to the service as mentioned here in Atlassian documentation:
curl --request GET
--url 'https://your-domain.atlassian.net/wiki/rest/api/search?cql={cql}'
--header 'Authorization: Bearer <access_token>'
--header 'Accept: application/json'

Related

Curl command line to import xml file to Xray

Good afternoon,
I'm using robotframework to run some tests.
After I run them I have an output.xml file with the results.
I searched ways to import these results to Xray and found these links:
https://docs.getxray.app/display/XRAY/Testing+using+Robot+Framework+integration+in+Python+or+Java
https://docs.getxray.app/display/XRAY/Import+Execution+Results+-+REST#ImportExecutionResultsREST-RobotFrameworkXMLresults
So I created a .sh file with this command line:
#!/bin/bash
PROJECT=myproject
TESTPLAN=mytestplan
curl -X POST -H "Content-Type: multipart/form-data" -u myuser:mypassword -F "file=output.xml" "https://myserver/rest/raven/1.0/import/execution/robot?projectKey=$PROJECT&testPlanKey=$TESTPLAN"
It displays this error '' Forbidden (403)''.
Do you know how to solve this?
I guess you're using Xray on Jira server/Data Center and not Jira Cloud, correct?
Is so, it should be something like:
curl -H "Content-Type: multipart/form-data" -u admin:admin -F "file=#output.xml" "http://<jira_base_url>/rest/raven/1.0/import/execution/robot?projectKey=ROB&testPlanKey=ROB-12&testEnvironments=$BROWSER"
Note that sometimes <jira_base_url> is something like http://<some_ip>/jira .. is it your case perhaps?
Note: In this tutorial, you can find a concrete example for Xray on Jira server/DC. A similar tutorial for Xray on Jira Cloud can be found here.

devpi: manually upload toxresult.json

I currently have a Jenkins pipeline which builds and tests my python package using tox. If all unittests pass, it will be uploaded to my local devpi index.
Using devpi test <mypackage> I can attach the test results to the release file on the index.
But this will download the already built package again, repeat all of the already passed test suites defined in the tox.ini file and only then upload the results in form of a toxresult.json.
Is there any way to directly upload the toxresult.json alongside the release files?
According to the quickstart and the documentation of test command there seems to be no command line option, and neither in the upload command.
Of course I could change my Jenkins pipeline to skip the tests before uploading and then build, upload and test the package using devpi. If the devpi test command fails I can remove the package from the index.
But I would rather not upload a package with failing tests in the first place.
The anonymous uploads
It's relatively easy if you allow the anonymous user to upload test results (which is the default setting IIRC). Make a POST request to the URL of the uploaded dist, passing tox results as JSON payload. Example:
$ curl -i \
-H "content-type: application/json" \
-X POST \
--data-binary "#/tmp/toxreport.json" \
http://my-server/myuser/myindex/+f/19b/d3544d03b1716/mypkg-1.0.tar.gz
On success, you should get a result similar to
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Content-Length: 143
Content-Type: application/json
Date: Wed, 08 Jan 2020 15:48:32 GMT
Server: waitress
X-Devpi-Api-Version: 2
X-Devpi-Master-Uuid: d800735d04a14c2d9bde920149cb8dbc
X-Devpi-Serial: 42
X-Devpi-Server-Version: 5.3.1
X-Devpi-Uuid: d800735d04a14c2d9bde920149cb8dbc
{
"result": "myuser/myindex/+f/19b/d3544d03b1716/mypkg-1.0.tar.gz.toxresult-20200108154832-0",
"type": "toxresultpath"
}
You can find the target URL in the File column of the files table on the project page. Or query the JSON API and filter the results, e.g.
$ devpi getjson /myuser/myindex/mypkg | jq -r '[ .result[] | .["+links"][] | .href ]'
Authenticated uploads
devpi uses basic auth, so simply pass the base64-encoded credentials in the Authorization: Basic header. Example, with curl again:
$ curl -i \
--user myuser:mypass \
-H "content-type: application/json" \
-X POST \
--data-binary "#/tmp/toxreport.json" \
http://my-server/myuser/myindex/+f/19b/d3544d03b1716/mypkg-1.0.tar.gz
If you need details on the test upload authentication, check out my other answer here.

Equivalent python code for curl command for https call

I have a curl command that works and gives me back the JSON.
Curl command:
curl -sS -k -L -H "Authorization: bearer <token>" -X GET https://IP:PORT/api/v1/namespaces
I tried with requestsand pycurl modules which I found in the other posts but no luck.
Can anyone help me with finding the equivalent in python???
We can do this with requests like this:
import requests
header = {'Authorization': 'bearer <token>'}
resp = requests.get("https://IP:PORT/api/v1/namespaces",
headers = header,
verify=False)
print(resp.status_code)
print(resp.text)
The -H switch behaviour is replicated by sending a header
The -L switch behaviour is replicated by specifying verify=False
The -sS and -k are about the behaviour of curl and not relevant here
The -X GET is replicated by using requests.get()

Using a python string output in curl

I have this python script
users=['mark','john','steve']
text=''
for user in users:
text+=str(user + " ")
print(text)
I want to output that string "text" into a curl terminal command.
I tried:
curl -d "#python-scirpt.py" --insecure -i -X POST https://10.10.10.6/hooks/84kk9emcdigz8xta1bykiymn5e
and
curl --insecure -i -X POST -H 'Content-Type: application/json' -d '{"text": 'python /home/scripts/python-script.py'}' https://10.10.10.6/hooks/84kk9emcdigz8xta1bykiymn5e
or without the quotations in the text option
Everything returns this error
{"id":"Unable to parse incoming data","message":"Unable to parse incoming data","detailed_error":"","request_id":"fpnmfds8zifziyc85oe5eyf3pa","status_code":400}
How to approach this ? Any help is appreciated thank you.
another approach would be to curl inside python but would need help in that too.
Thank you
Use command substitution (i.e. $(...)) to make the shell run the python code first.
So
curl -d "$(python-scirpt.py)" --insecure -i -X POST https://10.10.10.6/hooks/84kk9emcdigz8xta1bykiymn5e

How to programmatically set the github repo inside jenkins job configuration page

I want to set the Github repository url in http://host-ip:8080/job/my_project/configure jenkins page via code each time I spawn a new Jenkins container.
I read that this can be done with python-jenkins's reconfig_job function by replacing the config.xml .
Well how would I do that?
You have some clues in "How can I update a jenkins job using the api?"
For instance, since you spawn a new Jenkins container, you can docker cp an updated config.xml to the container (at the right path for that job)
(the OP Kostas Demiris confirms in the comments it works, when run in git bash)
You can also use one of the Jenkins API libraries, but check if a simple curl is enough first
#Get the current configuration and save it locally
curl -X GET http://user:password#jenkins.server.org/job/myjobname/config.xml -o mylocalconfig.xml
#Update the configuration via posting a local configuration file
curl -X POST http://user:password#jenkins.server.org/job/myjobname/config.xml --data-binary "#mymodifiedlocalconfig.xml"
The updated Jenkins doc mentions (for updating just one parameter in an existing config job):
Simple example - sending "String Parameters":
curl -X POST JENKINS_URL/job/JOB_NAME/build \
--data token=TOKEN \
--data-urlencode json='{"parameter": [{"name":"id", "value":"123"}, {"name":"verbosity", "value":"high"}]}'
Another example - sending a "File Parameter":
curl -X POST JENKINS_URL/job/JOB_NAME/build \
--user USER:PASSWORD \
--form file0=#PATH_TO_FILE \
--form json='{"parameter": [{"name":"FILE_LOCATION_AS_SET_IN_JENKINS", "file":"file0"}]}'

Categories

Resources