繁体   English   中英

请求无效。 详细信息:参数:无效的 JSON。 在 JSON 内容中无法识别令牌

[英]The request is invalid. Details: parameters : Invalid JSON. A token was not recognized in the JSON content

我正在尝试使用 django 发布请求发布到 azure 搜索服务。 我遇到一个问题,说我的 JSON 无效,但它通过了 JSON 验证器中的验证。

这是我的 function。

def uploaddd(request):

    url = 'https://hs.search.windows.net/indexes/hotels/docs/index?api-version=2020-06-30'
    
    data = {
        "value": {
            "@search.action": "upload",
            "HotelId": "133",
            "HotelName": "Secret Point Motel",
            "Description": "The hotel is ideally located on the main commercial artery of the city in the heart of New York. A few minutes away is Time's Square and the historic centre of the city, as well as other places of interest that make New York one of America's most attractive and cosmopolitan cities.",
            "Description_fr": "L'hôtel est idéalement situé sur la principale artère commerciale de la ville en plein cœur de New York. A quelques minutes se trouve la place du temps et le centre historique de la ville, ainsi que d'autres lieux d'intérêt qui font de New York l'une des villes les plus attractives et cosmopolites de l'Amérique.",
            "Category": "Boutique",
            "Tags": ["pool", "air conditioning", "concierge"],
            "ParkingIncluded": "false",
            "LastRenovationDate": "1970-01-18T00:00:00Z",
            "Rating": 3.60,
            "Address": {
                "StreetAddress": "677 5th Ave",
                "City": "New York",
                "StateProvince": "NY",
                "PostalCode": "10022",
                "Country": "USA"
            }
        }
    }
    
    headers = {'Content-Type': 'application/json', 'api-key': 'key'}

    response = requests.request('POST', url, headers=headers, data = data)
    return HttpResponse(response)

这是完整的错误

请求无效。 详细信息:参数:无效的 JSON。 在 JSON 内容中无法识别令牌。

根据REST API documentationvalue元素实际上是一个数组。 基于此,您能否尝试使用以下 JSON 有效负载:

{
    "value": [ 
        {
            "@search.action": "upload",
            "HotelId": "133",
            "HotelName": "Secret Point Motel",
            "Description": "The hotel is ideally located on the main commercial artery of the city in the heart of New York. A few minutes away is Time's Square and the historic centre of the city, as well as other places of interest that make New York one of America's most attractive and cosmopolitan cities.",
            "Description_fr": "L'hôtel est idéalement situé sur la principale artère commerciale de la ville en plein cœur de New York. A quelques minutes se trouve la place du temps et le centre historique de la ville, ainsi que d'autres lieux d'intérêt qui font de New York l'une des villes les plus attractives et cosmopolites de l'Amérique.",
            "Category": "Boutique",
            "Tags": ["pool", "air conditioning", "concierge"],
            "ParkingIncluded": "false",
            "LastRenovationDate": "1970-01-18T00:00:00Z",
            "Rating": 3.60,
            "Address": {
                "StreetAddress": "677 5th Ave",
                "City": "New York",
                "StateProvince": "NY",
                "PostalCode": "10022",
                "Country": "USA"
            }
        }
    ]
}

另外,请更改以下代码行:

response = requests.request('POST', url, headers=headers, data=data)

response = requests.request('POST', url, headers=headers, data=json.dumps(data))

这是我使用的完整代码:

import requests, json

url = 'https://account-name.search.windows.net/indexes/hotels/docs/index?api-version=2020-06-30'

data = {
    "value": [{
        "@search.action": "upload",
        "HotelId": "133",
        "HotelName": "Secret Point Motel",
        "Description": "The hotel is ideally located on the main commercial artery of the city in the heart of New York. A few minutes away is Time's Square and the historic centre of the city, as well as other places of interest that make New York one of America's most attractive and cosmopolitan cities.",
        "Category": "Boutique",
        "Tags": ["pool", "air conditioning", "concierge"],
        "ParkingIncluded": "false",
        "LastRenovationDate": "1970-01-18T00:00:00Z",
        "Rating": 3.60,
        "Address": {
            "StreetAddress": "677 5th Ave",
            "City": "New York",
            "StateProvince": "NY",
            "PostalCode": "10022",
            "Country": "USA"
        }
    }]
}

print data

headers = {'Content-Type': 'application/json', 'api-key': 'key'}

response = requests.request('POST', url, headers=headers, data=json.dumps(data))

print response.text

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM