“Batch request to Microsoft Graph returns ‘message’: ‘Url specified is invalid.'”

huangapple go评论65阅读模式
英文:

Batch request to Microsoft Graph returns "message": "Url specified is invalid."

问题

以下是您提供的代码部分的翻译:

{
  "requests": [
    {
      "id": "01GNYB5KKIKZVLU4SXJZAI6PD76QD3PUVQ",
      "method": "GET",
      "url": "https://graph.microsoft.com/v1.0/sites/bpmgroup.sharepoint.com,23e7ef7a-a529-4dde-81ba-67afb4f44401,0fa8e0f7-1c76-4ad0-9b6e-a485f9bfd63c/drive/items/01GNYB5KKIKZVLU4SXJZAI6PD76QD3PUVQ/thumbnails?select=large"
    },
    {
      "id": "01GNYB5KNZCQTFLFPNQNCJZXQVQWIBQWLG",
      "method": "GET",
      "url": "https://graph.microsoft.com/v1.0/sites/bpmgroup.sharepoint.com,23e7ef7a-a529-4dde-81ba-67afb4f44401,0fa8e0f7-1c76-4ad0-9b6e-a485f9bfd63c/drive/items/01GNYB5KNZCQTFLFPNQNCJZXQVQWIBQWLG/thumbnails?select=large"
    }
  ]
}

以下是您提供的响应部分的翻译:

{
  "responses": [
    {
      "id": "01GNYB5KNZCQTFLFPNQNCJZXQVQWIBQWLG",
      "status": 400,
      "headers": {
        "Content-Type": "application/json"
      },
      "body": {
        "error": {
          "code": "BadRequest",
          "message": "Url specified is invalid.",
          "innerError": {
            "date": "2023-05-17T18:17:39",
            "request-id": "7bb48670-f436-4c16-8137-9d70abe92dab",
            "client-request-id": "7bb48670-f436-4c16-8137-9d70abe92dab"
          }
        }
      }
    },
    {
      "id": "01GNYB5KKIKZVLU4SXJZAI6PD76QD3PUVQ",
      "status": 400,
      "headers": {
        "Content-Type": "application/json"
      },
      "body": {
        "error": {
          "code": "BadRequest",
          "message": "Url specified is invalid.",
          "innerError": {
            "date": "2023-05-17T18:17:39",
            "request-id": "7bb48670-f436-4c16-8137-9d70abe92dab",
            "client-request-id": "7bb48670-f436-4c16-8137-9d70abe92dab"
          }
        }
      }
    }
  ]
}

请注意,这些翻译是您提供的代码和响应的直译翻译,不包含其他信息。如果您需要进一步的帮助或解释,请随时提问。

英文:
{
  requests: [
        {
      id: '01GNYB5KKIKZVLU4SXJZAI6PD76QD3PUVQ',
      method: 'GET',
      url: 'https://graph.microsoft.com/v1.0/sites/bpmgroup.sharepoint.com,23e7ef7a-a529-4dde-81ba-67afb4f44401,0fa8e0f7-1c76-4ad0-9b6e-a485f9bfd63c/drive/items/01GNYB5KKIKZVLU4SXJZAI6PD76QD3PUVQ/thumbnails?select=large'        
        },
        {
      id: '01GNYB5KNZCQTFLFPNQNCJZXQVQWIBQWLG',
      method: 'GET',
      url: 'https://graph.microsoft.com/v1.0/sites/bpmgroup.sharepoint.com,23e7ef7a-a529-4dde-81ba-67afb4f44401,0fa8e0f7-1c76-4ad0-9b6e-a485f9bfd63c/drive/items/01GNYB5KNZCQTFLFPNQNCJZXQVQWIBQWLG/thumbnails?select=large'        
        }
    ]
}

Above is the batchPayload (testing with only 2 items) passed to my callMsGraph(token, batchUrl, batchPayload) function. And below is the received batchResponse:

{
    "responses": [
        {
            "id": "01GNYB5KNZCQTFLFPNQNCJZXQVQWIBQWLG",
            "status": 400,
            "headers": {
                "Content-Type": "application/json"
            },
            "body": {
                "error": {
                    "code": "BadRequest",
                    "message": "Url specified is invalid.",
                    "innerError": {
                        "date": "2023-05-17T18:17:39",
                        "request-id": "7bb48670-f436-4c16-8137-9d70abe92dab",
                        "client-request-id": "7bb48670-f436-4c16-8137-9d70abe92dab"
                    }
                }
            }
        },
        {
            "id": "01GNYB5KKIKZVLU4SXJZAI6PD76QD3PUVQ",
            "status": 400,
            "headers": {
                "Content-Type": "application/json"
            },
            "body": {
                "error": {
                    "code": "BadRequest",
                    "message": "Url specified is invalid.",
                    "innerError": {
                        "date": "2023-05-17T18:17:39",
                        "request-id": "7bb48670-f436-4c16-8137-9d70abe92dab",
                        "client-request-id": "7bb48670-f436-4c16-8137-9d70abe92dab"
                    }
                }
            }
        }
    ]
}

I have tested the urls as individual GET requests in postman, and they are indeed valid. But the POST response to batchUrl https://graph.microsoft.com/v1.0/$batch is returning that they are invalid?

I don't see how the URL could be malformed in anyway. I initially called each URL separately (without batching) with callMsGraph(accessToken, graphEndpoint) and the responses were fine -- the URLs are unchanged. I ofcourse want to utilize batching to reduce individual network requests.

My callMsGraph:

function callMsGraph(accessToken, graphEndpoint, payload) {
    const headers = new Headers();
    const bearer = `Bearer ${accessToken}`;

    headers.append("Authorization", bearer);
    headers.append("Content-Type", "application/json");

    const options = {
        method: "POST",
        headers: headers,
        body: JSON.stringify(payload)
    };

    return fetch(graphEndpoint, options)
        .then(response => response.json())
    ....
    ...

答案1

得分: 1

在批量请求中,您的URL应该是相对的 https://learn.microsoft.com/en-us/graph/json-batching

> 必需的。这是个相对资源URL,通常用于发送单个请求。因此,虽然绝对URL是 https://graph.microsoft.com/v1.0/users,但这个URL是 /users。

所以类似于

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

{
requests: [
{
id: '01GNYB5KKIKZVLU4SXJZAI6PD76QD3PUVQ',
method: 'GET',
url: '/sites/bpmgroup.sharepoint.com,23e7ef7a-a529-4dde-81ba-67afb4f44401,0fa8e0f7-1c76-4ad0-9b6e-a485f9bfd63c/drive/items/01GNYB5KKIKZVLU4SXJZAI6PD76QD3PUVQ/thumbnails?select=large'
},
{
id: '01GNYB5KNZCQTFLFPNQNCJZXQVQWIBQWLG',
method: 'GET',
url: '/sites/bpmgroup.sharepoint.com,23e7ef7a-a529-4dde-81ba-67afb4f44401,0fa8e0f7-1c76-4ad0-9b6e-a485f9bfd63c/drive/items/01GNYB5KNZCQTFLFPNQNCJZXQVQWIBQWLG/thumbnails?select=large'
}
]
}

<!-- end snippet -->

英文:

In a batch request you URL's should be relative https://learn.microsoft.com/en-us/graph/json-batching

> Required. The relative resource URL the individual request would typically be sent to. Therefore, while the absolute URL is https://graph.microsoft.com/v1.0/users, this url is /users.

so something like

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

{
  requests: [
        {
      id: &#39;01GNYB5KKIKZVLU4SXJZAI6PD76QD3PUVQ&#39;,
      method: &#39;GET&#39;,
      url: &#39;/sites/bpmgroup.sharepoint.com,23e7ef7a-a529-4dde-81ba-67afb4f44401,0fa8e0f7-1c76-4ad0-9b6e-a485f9bfd63c/drive/items/01GNYB5KKIKZVLU4SXJZAI6PD76QD3PUVQ/thumbnails?select=large&#39;        
        },
        {
      id: &#39;01GNYB5KNZCQTFLFPNQNCJZXQVQWIBQWLG&#39;,
      method: &#39;GET&#39;,
      url: &#39;/sites/bpmgroup.sharepoint.com,23e7ef7a-a529-4dde-81ba-67afb4f44401,0fa8e0f7-1c76-4ad0-9b6e-a485f9bfd63c/drive/items/01GNYB5KNZCQTFLFPNQNCJZXQVQWIBQWLG/thumbnails?select=large&#39;        
        }
    ]
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月18日 03:05:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定