curl equivalent to python POST requests to call jenkins api - python-3.x

I am trying to call jenkins api to build a job using curl command which perfectly fine. Here is my curl command:
curl -X POST https://myjenkinshost/job/myorg/job/10042/job/myproject/job/10042_myansiblejob/build --user admuser:my-token --data-urlencode json='{"parameter": [{"name": "ENVIRONNEMENT", "value": "developpement"},{"name": "REFERENCE_GIT_INVENTAIRE", "value": "develop"},{"name": "REFERENCE_GIT_PLAYBOOK", "value": "develop"},{"name": "VAULT", "value": "mysecretpassword"},{"name": "EXTRA_VARS", "value": "myhosts: TARGET"}]}'
But when I try this with python, it always shows HTTP 400 :
Error:
Reason: HTTP ERROR 400. Problem accessing /job/10042/job/myproject/job/10042_myansiblejob/build. Nothing is submitted
Here is my python code which is very simple, could be a very small issue.
import requests
import json
basicAuthCredentials = ('admuser', 'my-token')
jenkins_headers={'Content-type':'application/json', 'Accept':'application/json'}
ansible_vault_password="mysecretpassword"
JENKINS_URL="https://myjenkinshost/job/myorg/job/10042/job/myproject/job/10042_myansiblejob/build"
ENVIRONNEMENT="developpement"
GIT_BRANCH="develop"
ANSIBLE_EXTRA_VARS_VARNAME="myhosts: TARGET"
json_payload='{"parameter": [{"name": "ENVIRONNEMENT", "value": "'+ENVIRONNEMENT+'"'+ \
'},{"name": "REFERENCE_GIT_INVENTAIRE", "value": "'+GIT_BRANCH+'"'+ \
'},{"name": "REFERENCE_GIT_PLAYBOOK", "value": "'+GIT_BRANCH+'"'+ \
'},{"name": "VAULT", "value": "'+ansible_vault_password+'"'+ \
'},{"name": "EXTRA_VARS", "value": "'+ANSIBLE_EXTRA_VARS_VARNAME+'"}]}'
json_data=json.dumps(json_payload)
response_jenkins = requests.post(JENKINS_URL, headers=jenkins_headers,
data=json_data, auth=basicAuthCredentials)
print(response_jenkins.text)
Reference: https://www.jenkins.io/doc/book/using/remote-access-api/
Any suggestion is appreciated.

From curl command you are doing --data-urlencode, which is sending Content-Type: application/x-www-form-urlencoded.
From your python you are passing Content-type':'application/json.

Related

github's post pulls api throwing invalid head error for a forked head in same org

repo1: https://github.com/ORG_SPACE/MAIN_REPO
repo2(forked from repo1 in same workspace): https://github.com/ORG_SPACE/FORKED_REPO
feature branch created in forked repo: https://github.com/ORG_SPACE/FORKED_REPO/tree/FEATURE_BRANCH
Authorization details: used new finegrained PAT having pull:read&write,contents:read,Metadata:read access of both repos MAIN_REPO,FORKED_REPO
ISSUE: getting 422 error response
{
"message": "Validation Failed",
"errors": [
{
"resource": "PullRequest",
"field": "head",
"code": "invalid"
}
],
"documentation_url": "https://docs.github.com/rest/reference/pulls#create-a-pull-request"
}
while hitting below curl to raise pull request to merge 'ORG_SPACE/FORKED_REPO/tree/FEATURE_BRANCH' into 'ORG_SPACE/MAIN_REPO/tree/master'
curl --location --request POST 'https://api.github.com/repos/ORG_SPACE/MAIN_REPO/pulls' \
--header 'Authorization: Bearer MY_PAT' \
--header 'Accept: application/vnd.github+json' \
--header 'Content-Type: application/json' \
--data-raw '{
"title": "test",
"head": "ORG_SPACE:FEATURE_BRANCH",
"base": "master"
}'
tried this head value : "ORG_SPACE:FORKED_REPO/FEATURE_BRANCH" but didn't work.
raising pull request with same api working fine for both repos while head and base are in same repo.

Creating invoices in bulk using NetSuite REST API

Is there a way to create multiple invoices with a single POST request to the NetSuite REST API?
The API Reference doesn't mention anything - but I'm hoping there's a way to avoid sending thousands of individual requests.
I am creating a single invoice like this (some headers have been removed):
curl --location --request POST 'https://{{COMPANY_URL}}.suitetalk.api.netsuite.com/services/rest/record/v1/invoice' \
--header 'Content-Type: application/json' \
--data-raw '{
"entity": {
"id": 46
},
"item": {
"items": [
{
"item": {
"id": 134
},
"amount": 20
}
]
}
}'

create github organisation using API

I am trying to create organisation using github enterprise API, i am following this -
https://docs.github.com/en/enterprise-server#2.22/rest/reference/enterprise-admin#create-an-organization
Below is the API tried :
curl -u acme-admin:token -X POST -H "Accept: application/vnd.github.v3+json" https://acme.example.com/api/v3/admin/organizations -d '{"login":"acme-admin","admin":"acme-admin","profile_name":"exampleorg"}'
Response is as shown below, do any one have any clue about this ? the user "acme-admin" exists in the github enterprise and this user is administrator only.
{
"message": "Validation Failed",
"errors": [
{
"resource": "Organization",
"code": "custom",
"field": "login",
"message": "login is not available"
},
{
"resource": "Organization",
"code": "missing_field",
"field": "admins"
}
],
"documentation_url": "https://docs.github.com/enterprise/2.22/user/rest/reference/enterprise-admin#create-an-organization"
}
A similar script mentions:
curl -i -H "Authorization: token $gitub_api_token" ...
You can see that header defined in "REST API / Other authentication methods".
Check if this works better than -u acme-admin:token.

Microsoft Graph API: SharePoint: Get items list in "default Drive" of "root Site"

Overview
I am familiar in using Microsoft Graph API with File scoping for handling items within root directory if (items specifically folders and files) in Microsoft OneDrive; create/delete/metadata for Folders and upload/delete/metadata for Files.
Using Microsoft Graph API again, I wish to do perform the same actions in handling Folders and Files, create/delete/metadata for folders and upload/delete/metadata for files, in Microsoft SharePoint within the default Drive of root Site as mention prior for OneDrive.
Problem
I having the following issues with Microsoft Graph API in using for handling items within default Drive or root Site:
Using default Drive ID in handling items within a site's drive.
Create/Delete/Get-Metadata of a folder item within default Site's Drive.
Upload/Delete/Get-Metadata of a file item within a parent folder.
The following Microsoft Graph API call returns root site's default drive's metadata**:
curl "https://graph.microsoft.com/v1.0/sites/root/drive" \
--request GET \
--verbose \
--write-out 'HTTPSTATUS:%{http_code}' \
--silent \
--header "authorization: Bearer [** ACCESS_TOKEN **]" \
--header "Content-Type: application/json"
JSON Response example is:
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives/$entity",
"createdDateTime": "2012-06-12T17:27:56Z",
"description": "Share a document with the team by adding it to this document library.",
"id": "b!mWxqgLJ9mESqZI4PrP0Gs-F4hgLaCRlCkVuON4nbwzhkKbcyWdM1Tb5WEzNJ0C60",
"lastModifiedDateTime": "2015-03-03T02:39:56Z",
"name": "Shared Documents",
"webUrl": "https://docusign2com.sharepoint.com/Shared%20Documents",
"driveType": "documentLibrary",
"createdBy": {
"user": {
"displayName": "System Account"
}
},
"lastModifiedBy": {
"user": {
"displayName": "System Account"
}
},
"quota": {
"deleted": 0,
"remaining": 0,
"total": 0,
"used": 0
}
}
Problem Getting Drive Metadata
Taking the unencoded drive_id for the current default Drive in root Site, [** DRIVE_ID **]:
"id": "b!mWxqgLJ9mESqZI4PrP0Gs-F4hgLaCRlCkVuON4nbwzhkKbcyWdM1Tb5WEzNJ0C60"
Encode drive_id, [** URL-Encoded DRIVE_ID **]:
"id": "b%21mWxqgLJ9mESqZI4PrP0Gs-F4hgLaCRlCkVuON4nbwzhkKbcyWdM1Tb5WEzNJ0C60"
Using the encoded drive_id, we can get the metadata for that drive another way:
curl "https://graph.microsoft.com/v1.0/sites/root/drives/[** URL-Encoded DRIVE_ID **]" \
--request GET \
--verbose \
--header "authorization: Bearer [** ACCESS_TOKEN **]" \
--header "Content-Type: application/json"
Response:
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives/$entity",
"createdDateTime": "2012-06-12T17:27:56Z",
"description": "Share a document with the team by adding it to this document library.",
"id": "[** DRIVE_ID **]",
"lastModifiedDateTime": "2015-03-03T02:39:56Z",
"name": "Shared Documents",
"webUrl": "https://docusign2com.sharepoint.com/Shared%20Documents",
"driveType": "documentLibrary",
"createdBy": {
"user": {
"displayName": "System Account"
}
},
"lastModifiedBy": {
"user": {
"displayName": "System Account"
}
},
"quota": {
"deleted": 0,
"remaining": 0,
"total": 0,
"used": 0
}
}
Next, I tried listing the items within the root Site's default Drive by appending /items:
curl "https://graph.microsoft.com/v1.0/sites/root/drives/[** URL-Encoded DRIVE_ID **]/items" \
--request GET \
--verbose \
--header "authorization: Bearer [** ACCESS_TOKEN **" \
--header "Content-Type: application/json"
And it fails:
{
"error": {
"code": "invalidRequest",
"message": "The request is malformed or incorrect.",
"innerError": {
"request-id": "0a212014-b386-45d9-9c36-bae2dd6cea8f",
"date": "2020-02-07T06:51:15"
}
}
}
API Path Requested
What is the expected path for getting the list of all items within a default drive's root within root site?
Thank you
Instead of using .../v1.0/sites/$site_id/drives/... for working with drives, I switched to using .../v1.0/drives/$drive_id/..., and this worked.
The following Microsoft Graph API request above using /v1.0/sites/root/drives/[** URL-Encoded DRIVE_ID **], the return previously acquired default drive (identified by [** DRIVE_ID **]) metadata:
curl "https://graph.microsoft.com/v1.0/drives/[** url-encoded DRIVE_ID **]" \
--request GET \
--verbose \
--silent \
--header "authorization: Bearer [** ACCESS_TOKEN **]" \
--header "Content-Type: application/json"
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives/$entity",
"createdDateTime": "2012-06-12T17:27:56Z",
"description": "Share a document with the team by adding it to this document library.",
"id": "[** DRIVE_ID **]",
"lastModifiedDateTime": "2015-03-03T02:39:56Z",
"name": "Shared Documents",
"webUrl": "https://docusign2com.sharepoint.com/Shared%20Documents",
"driveType": "documentLibrary",
"createdBy": {
"user": {
"displayName": "System Account"
}
},
"lastModifiedBy": {
"user": {
"displayName": "System Account"
}
},
"quota": {
"deleted": 0,
"remaining": 0,
"total": 0,
"used": 0
}
}
This returns the root folder of default drive:
curl "https://graph.microsoft.com/v1.0/drives/[** url-encoded DRIVE_ID **]/root" \
--request GET \
--verbose \
--silent \
--header "authorization: Bearer [** ACCESS_TOKEN **]" \
--header "Content-Type: application/json"
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives('[** url-encoded DRIVE_ID **]')/root/$entity",
"createdDateTime": "2012-06-12T17:27:56Z",
"id": "[** ROOT_FOLDER_ID **]",
"lastModifiedDateTime": "2020-02-07T16:04:09Z",
"name": "root",
"webUrl": "https://docusign2com.sharepoint.com/Shared%20Documents",
"size": 27781340,
"parentReference": {
"driveId": "b!mWxqgLJ9mESqZI4PrP0Gs-F4hgLaCRlCkVuON4nbwzhkKbcyWdM1Tb5WEzNJ0C60",
"driveType": "documentLibrary"
},
"fileSystemInfo": {
"createdDateTime": "2012-06-12T17:27:56Z",
"lastModifiedDateTime": "2020-02-07T16:04:09Z"
},
"folder": {
"childCount": 5
},
"root": {}
}
Now with root folder of default drive, the list of children folders can be requested:
curl "https://graph.microsoft.com/v1.0/drives/[** url-encoded DRIVE_ID **]/items/[** ROOT_FOLDER_ID **]/children" \
--request GET \
--verbose \
--silent \
--header "authorization: Bearer [** ACCESS_TOKEN **]" \
--header "Content-Type: application/json"

Dialogflow v2 REST call returns 400 Bad Request

I get a 400 Bad Request when issuing this entityTypes.create call:
wget --header="Authorization: Bearer ${TOKEN}" \
--post-data=' { "displayName": "writer1", "kind": "KIND_MAP", "autoExpansionMode": "AUTO_EXPANSION_MODE_DEFAULT", "entities": [ { "value": "Virginia Woolf", "synonyms": [ "Virginia Woolf" ] } }' \
https://dialogflow.googleapis.com/v2/projects/MyProject/agent/entityTypes
but this -- entityTypes.list -- works OK:
wget --header="Authorization: Bearer ${TOKEN}" \
https://dialogflow.googleapis.com/v2/projects/MyProject/agent/entityTypes
Any ideas? Thanks.
By default, --post-data uses a content-type of application/x-www-form-urlencoded. The API requires the content-type of application/json.
I tend to use something more like
wget \
--header="Authorization: Bearer ${TOKEN}" \
--header="Content-type: application/json" \
--post-data='{ ... }' \
https://dialogflow.googleapis.com/v2/projects/MyProject/agent/entityTypes

Resources