Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Artifactory AQL Query Using Python 3 'Requests'

Would anybody be able to help me identify where I am going wrong with a very basic AQL I am trying to execute using Python 3 and the 'Requests' library?

No matter what I do I cant seem to get past a 400: Bad Request. It is clearly something to do with the formatting of the data I am trying to Post but just can't see it. I'm assuming I want to pass it as a string as when posting sql queries they have to be in plain text. Just as an aside, I can execute this query absolutely fine using postman etc.

import requests
from requests.auth import HTTPBasicAuth
import json

HEADERS = {'Content-type': 'text'}
DATA =  'items.find({"type":"folder","repo":{"$eq":"BOS-Release-Builds"}}).sort({"$desc":["created"]}).limit(1)'

response = requests.post('https://local-Artifactory/artifactory/api/search/aql', headers=HEADERS, auth=HTTPBasicAuth('user', 'password'), data = DATA)
print (response.status_code)
##print (response.json())
def jprint(obj):
    text = json.dumps(obj, sort_keys=True, indent=4)
    print (text)

jprint(response.json())
print(response.url)
print(response.encoding)
like image 240
SVye Avatar asked Oct 15 '25 20:10

SVye


1 Answers

Ok, after sitting staring at the code for another few minutes I spotted my mistake. I shouldnt have defined the Header content type in the request and instead let the 'Requests' library deal with this for you.

So the code should look like:

import requests
from requests.auth import HTTPBasicAuth
import json

DATA =  'items.find({"type":"folder","repo":{"$eq":"BOS-Release-Builds"}}).sort({"$desc":["created"]}).limit(1)'

response = requests.post('https://uk-artifactory.flowbird.group/artifactory/api/search/aql', auth=HTTPBasicAuth('user', 'password!'), data = DATA)
print (response.status_code)
##print (response.json())
def jprint(obj):
    text = json.dumps(obj, sort_keys=True, indent=4)
    print (text)

jprint(response.json())
print(response.url)
print(response.encoding)
like image 155
SVye Avatar answered Oct 18 '25 16:10

SVye



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!