415 Unsupported Media Type in Artifactory AQL POST

Viewed 6180

Probably a simple mistake but I get a 415 Unsupported Media Type error with this simple Artifactory AQL POST. I get the same error regardless of whether I include the content-type header.

#!/usr/local/bin/python
import requests
import json

username = "admin"
password = "password"
url = "http://myhost:8081/artifactory/api/search/aql"

r = requests.post(url, auth=(username, password), headers={"content-type":"application/json"}, json='{items.find( { "repo":{"$eq":"test-repo"} })}')

if r.status_code == 200:
    print "Success!\n"
    print r.content
else:
    print "Fail\n"
    print r.text

{ "errors" : [ { "status" : 415, "message" : "Unsupported Media Type" } ] }

2 Answers

AQL is not JSON. The text inside the items.find(...) is formatted as JSON, but the entire query as a whole doesn't follow the JSON standard. The expected content type is text/plain.

Also, instead of json='{items.find( { "repo":{"$eq":"test-repo"} })}', you should use data='items.find( { "repo":{"$eq":"test-repo"} })'.

I stumbled at the same issue, it worked when I changed the content type to 'text/plain'. Just to augment @DarthFennec's answer, providing what the official REST API documentation quotes:

Sample Usage:

POST /api/search/aql

items.find(
    {
        "repo":{"$eq":"libs-release-local"}
    }
)

Produces: application/json Sample Output:

{
    "results" : [
    {
        "repo" : "libs-release-local",
        "path" : "org/jfrog/artifactory",
        "name" : "artifactory.war",
        "type" : "item type",
        "size" : "75500000",
        "created" : "2015-01-01T10:10;10",
        "created_by" : "Jfrog",
        "modified" : "2015-01-01T10:10;10",
        "modified_by" : "Jfrog",
        "updated" : "2015-01-01T10:10;10"
    }
    ],
    "range" : {
    "start_pos" : 0,
    "end_pos" : 1,
    "total" : 1
    }
}

Note that the Content-type signifies the type(format) of the data in the request(so as per doc., it expects text/plain) while Accept notifies about the expected response(here, the artifactory will return json).

Related