4

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)
SVye
  • 71
  • 1
  • 4

1 Answers1

3

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)
SVye
  • 71
  • 1
  • 4