415 Artifactory AQL 中不支持的媒体类型 POST

415 Unsupported Media Type in Artifactory AQL POST

可能是一个简单的错误,但我在使用这个简单的 Artifactory AQL POST 时遇到了 415 Unsupported Media Type 错误。无论我是否包含 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" } ] }

A​​QL 不是 JSON。 items.find(...) 中的文本格式为 JSON,但整个查询并不遵循 JSON 标准。预期的内容类型是 text/plain.

此外,您应该使用 data='items.find( { "repo":{"$eq":"test-repo"} })'

而不是 json='{items.find( { "repo":{"$eq":"test-repo"} })}'

我遇到了同样的问题,当我将内容类型更改为 'text/plain' 时它起作用了。只是为了增加@DarthFennec 的回答,提供官方 REST API documentation 引用的内容:

示例用法:

POST /api/search/aql

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

产生:application/json 示例输出:

{
    "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
    }
}

请注意,Content-type 表示请求中数据的类型(格式)(因此根据文档,它期望 text/plain),而 Accept 通知有关预期响应(此处为人工制品)将 return json).