Search REST API returning status code 500

lee sharepoint 80 Reputation points
2024-05-19T19:42:11.3333333+00:00

I am using search api with SPFx. The result ends up to server status code 500 Internal Server Error the response payload being the following:

{"odata.error":{"code":"-1, Microsoft.Office.Server.Search.REST.SearchServiceException","message":{"lang":"en-US","value":"An unknown error occurred."}}}

The code in the spfx:

  public render(): void {
    this._getSearchData("http://xxx.sharepoint.com/_api/search/query?querytext='*'").then((res: any) => {});
  }

  private _getSearchData(url: string): Promise<any> {
    return this.context.spHttpClient.get(url, SPHttpClient.configurations.v1, {
      headers: {
        'odata-version': '3.0',
        'accept': 'application/json;odata=verbose',
        'content-type': 'application/json;odata=verbose'
      }
    }).then((res: SPHttpClientResponse) => {
      return res.json();
    }).catch(error => {
      return Promise.reject(JSON.stringify(error));
    });
  }  


Please help fix

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,914 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,734 questions
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 32,401 Reputation points Microsoft Vendor
    2024-05-20T06:40:08.35+00:00

    Hi @lee sharepoint,

    It is a server error, not a client error. If server errors weren't to be returned to the client, there wouldn't have been created an entire status code class for them (i.e. 5xx).

    You can't hide the fact that you either made a programming error or some service you rely on is unavailable, and that certainly isn't the client's fault. Returning any other range of code in those cases than the 5xx series would make no sense.

    RFC 7231 mentions in section 6.6. Server Error 5xx:

    The 5xx (Server Error) class of status code indicates that the server is aware that it has erred or is incapable of performing the requested method.

    This is exactly the case. There's nothing "internal" about the code "500 Internal Server Error" in the sense that it shouldn't be exposed to the client.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful