Error 500 when adding table to confluence page using rest api - python

I'm trying to add a table to a confluence page using the rest api but it returns error 500. I don't have permissions to check the server log so I'd just like to check if the issue is with my code before i ask the server admin for permissions.
`
import json
import requests
# Set the base URL for the Confluence REST API
base_url = "https://myconfluence.atlassian.net/wiki"
# Set the content ID
content_id = "30825808261"
# Set the table data
table_data = [
{
"type": "tableRow",
"content": [
{
"type": "tableCell",
"content": [
{
"type": "paragraph",
"text": "Row 1, Cell 1"
}
]
},
{
"type": "tableCell",
"content": [
{
"type": "paragraph",
"text": "Row 1, Cell 2"
}
]
}
]
},
{
"type": "tableRow",
"content": [
{
"type": "tableCell",
"content": [
{
"type": "paragraph",
"text": "Row 2, Cell 1"
}
]
},
{
"type": "tableCell",
"content": [
{
"type": "paragraph",
"text": "Row 2, Cell 2"
}
]
}
]
}
]
# Set the updated content, including the table
updated_content = {
"type": "page",
"content": [
{
"type": "paragraph"
},
{
"type": "table",
"content": table_data
}
],
"version": {
"number": 6
}
}
# Set the request headers
headers = {
"Content-Type": "application/json"
}
# Set the authentication credentials
username = 'username'
token = 'token'
auth = (username , token)
# Send the PUT request to update the content
response = requests.put(
f"{base_url}/rest/api/content/{content_id}",
headers=headers,
auth=auth,
data=json.dumps(updated_content)
)
print(updated_content)
print(response)
print(response.status_code)
# Check the status code of the response
if response.status_code == 200:
print("Content updated successfully")
else:
print("Error updating content")
`
This is the output i get:
<Response [500]>
500
Error updating content

Related

Netsuite Rest API returns "No Content" status (204) when completed successfully

i use the requests library. how can this be the default behavior? any way to return the ID of the item created?
def create_sales_order():
url = f"https://{url_account}.suitetalk.api.netsuite.com/services/rest/record/v1/salesOrder"
data = {
"entity": {
"id": "000"
},
"item": {
"items": [
{
"item": {
"id": 25
},
"quantity": 3,
"amount": 120
}
]
},
"memo": "give me money",
"Department": "109"
}
body = json.dumps(data)
response = client.post(url=url, headers=headers, data=body)
print(response.text)
Ok so it turns out that the header returned in the 204 empty response contains a link to the created item (Location is the key name in the json returned) , which is sufficient to do another get request and have all the info returned.

Unable to POST request correctly

I wanted to shorten the link through the wklej API, they have documentation on the site but only for php, I can’t understand what the problem is. Here is an example request from their website:
https://wklej.to/api/url/add
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://wklej.to/api/url/add",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 2,
CURLOPT_TIMEOUT => 10,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer HERE IS MY API KEY",
"Content-Type: application/json",
),
CURLOPT_POSTFIELDS => '{
"url": "https:\/\/google.com",
"custom": "google",
"password": "mypass",
"expiry": "2020-11-11 12:00:00",
"type": "splash",
"metatitle": "Not Google",
"metadescription": "Not Google description",
"metaimage": "https:\/\/www.mozilla.org\/media\/protocol\/img\/logos\/firefox\/browser\/og.4ad05d4125a5.png",
"geotarget": [
{
"location": "Canada",
"link": "https:\/\/google.ca"
},
{
"location": "United States",
"link": "https:\/\/google.us"
}
],
"devicetarget": [
{
"device": "iPhone",
"link": "https:\/\/google.com"
},
{
"device": "Android",
"link": "https:\/\/google.com"
}
],
"languagetarget": [
{
"language": "en",
"link": "https:\/\/google.com"
},
{
"language": "fr",
"link": "https:\/\/google.ca"
}
],
"parameters": [
{
"name": "aff",
"value": "3"
},
{
"device": "gtm_source",
"link": "api"
}
]
}',
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
I tried making a request like this:
r = requests.post("https://wklej.to/api/url/add", data = {"api": "HERE IS MY API KEY", "url": "https://amazon.com"})
but in response i get this:
{"error":1,"message":"A valid API key is required to use this service."}
I recently started learning python and would like to know what I did wrong, if I understand correctly then I need to send a request in JSON format. By the way, their documentation states that in the request you only need to specify a link for the shortener and an api key, everything else is optional.
update
now i do this:
headers = {
'Authorization': 'Bearer MY_API_KEY',
'Content-Type': 'application/json',
}
data = {"url": "https://amazon.com"}
r = requests.post("https://wklej.to/api/url/add", headers=headers, data=data)
and response is :
{"error":1,"message":"Missing required parameter: url"}
Why is it missing ? I have set this in "data"
Try this instead:
r = requests.post("https://wklej.to/api/url/add", headers=headers, json=data)
The data argument will be form-encoded, while the json argument serializes the passed value to json. See documentation here.

400 Bad Request POST request

I'm programing in Python some API application, using POSTMAN, and a Bearer token. I already receive the token, and to some GET with success response.
But when doing a insert of a record I got 400 Bad request error, this is the code I'm using for adding the record
def add_identity(token, accountid, newIdentity):
end_point = f"https://identityservice-demo.clearid.io/api/v2/accounts/{accountid}/identities/"
headers = CaseInsensitiveDict()
headers["Content-type"] = "application/json; charset=utf-8"
headers["Authorization"] = f"Bearer {token}"
response = requests.request("POST", end_point, data=newIdentity, headers=headers)
print(f"{response.reason} - {response.status_code}")
the variable newIdentity has the following data
nID = {
"privateData": {
"birthday": "1985-30-11T18:23:27.955Z",
"employeeNumber": "99999999",
"secondaryEmail": "",
"cityOfResidence": "Wakanda",
"stateOfResidence": "Florida",
"zipCode": "102837",
"phoneNumberPrimary": "(999)-999-999)",
"phoneNumberSecondary": "+5-(999)-999-9999"
},
"companyData": {
"approvers": [
{
"approverId": ""
}
],
"supervisorName": "Roger Rabbit",
"departmentName": "Presidency",
"jobTitle": "President",
"siteId": "string",
"companyName": "ACME Inc",
"workerTypeDescription": "",
"workerTypeCode": ""
},
"systemData": {
"hasExtendedTime": "true",
"activationDateUtc": "2022-03-16T18:23:27.955Z",
"expirationDateUtc": "2022-03-16T18:23:27.955Z",
"externalId": "999999",
"externalSyncTimeUtc": "2022-03-16T18:23:27.955Z",
"provisioningAttributes": [
{
"name": ""
}
],
"customFields": [
{
"customFieldType": "string",
"customFieldName": "SSNO",
"customFieldValue": "9999999"
}
]
},
"nationalIdentities": [
{
"nationalIdentityNumber": "0914356777",
"name": "Passport",
"issuer": "Wakanda"
}
],
"description": "1st Record ever",
"status": "Active",
"firstName": "Bruce",
"lastName": "Wayne",
"middleName": "Covid",
"displayName": "Bruce Wayne",
"countryCode": "WK",
"email": "bruce.wayne#wakanda.com",
"creationOnBehalf": "ACME"
}
what could solve the problem?
the swagger for the API is
https://identityservice-demo.clearid.io/swagger/index.html#/Identities/get_api_v2_accounts__accountId__identities
Thanks for your help in advance
data have to be a dict ,, you can try import json and data=json.dumps(newIdentity) ,
and if it keeps returning 400 , check well that all the parameters are accepted by the api by recreating the request with Postman or any request editor, and if the api uses any web interface check what is the reason for that 400 . This was translated by Google so I don't know if I said something nonsense :)

Python to wordpress

I've written this code for sending a cpt with metadata from python to WordPress, it`s one of 4 different scenarios that I have prove, practically I need to send a post with some meta field from python to WordPress using an external variable that set title and product price and product spec,
the JSON file that I need to do that asking to postman with a get request is that
[
{
"id": 3693,
"date": "2021-07-14T20:00:35",
"date_gmt": "2021-07-14T18:00:35",
"guid": {
"rendered": "http://www.mydomain.it/acme_product/spec-3/"
},
"modified": "2021-07-20T01:58:48",
"modified_gmt": "2021-07-19T23:58:48",
"slug": "spec-3",
"status": "publish",
"type": "acme_product",
"link": "http://www.mydomain.it/acme_product/spec-3/",
"title": {
"rendered": "spec"
},
"featured_media": 0,
"template": "",
"product_price": null,
"product_spec": null,
"_links": {
"self": [
{
"href": "http://www.mydomain.it/wp-json/wp/v2/acme_product/3693"
}
],
"collection": [
{
"href": "http://www.mydomain.it/wp-json/wp/v2/acme_product"
}
],
"about": [
{
"href": "http://www.mydomain.it/wp-json/wp/v2/types/acme_product"
}
],
"wp:attachment": [
{
"href": "http://www.mydomain.it/wp-json/wp/v2/media?parent=3693"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
},
when i actually use my python code wordpress accept the post but don't fill the field for product price and product specs , some idea, what can i do with that? thanks for all
from __future__ import print_function
import base64
import sys
from datetime import datetime
import json
import requests
user= 'username'
password='auth
url = 'http://www.mydomain.it/wp-json/wp/v2/'
d= sys.argv
a_dict={}
b_dict={}
data_string = user + ':' + password
token = base64.b64encode(data_string.encode())
headers = {'Authorization': 'Basic ' + token.decode('utf-8')}
date="2021-07-14T20:00:35"
title=d[1]
slug= d[1]
status= "publish"
author="1"
##excerpt="exc post"
##format="standard"
product_price= d[2]
product_spec=d[3]
for var in ["product_price","product_spec"]:
b_dict[var]= eval(var)
print (b_dict[var])
meta= b_dict[var]
for variable in ["date", "title", "slug","status","author","product_price","product_spec"]:
a_dict[variable] = eval(variable)
r = requests.post(url + 'acme_product', headers=headers, json=a_dict)
print(r.content.decode('utf-8'))
print('Your post is published on ' + json.loads(r.content.decode('utf-8'))['link'])

Can't get discord webhook to send with json

I've been trying to make a discord webhook, and for some reason I can't get it to send to save my life. I have all the embeds in a json file, and am trying to run it with python. Any help would be greatly appreciated.
json file:
{
"embeds": [
{
"title": "Title",
"color": 11393254,
"fields": [
{
"name": "Name1",
"value": "value1"
},
{
"name": "Name2",
"value": "value2",
"inline": true
},
],
"footer": {
"text": "Footer",
"icon_url": "random image url"
},
"timestamp": "now",
"thumbnail": {
"url": "random image url"
}
}
],
"username": "Username"
}
python code:
import requests, json
with open('webhook.json') as json_file:
data = json.load(json_file)
url = 'https://discord.com/api/webhooks/xxxxx'
headers={"Content-Type": "application/json"}
requests.post(url, data=json.dumps(data), headers=headers)
What's the status code you get back from the POST? That could be a clue where it's going awry.

Categories

Resources