Azure AI Search REST API failing in Angular

Dey, Nikita 20 Reputation points
2023-11-16T11:09:51.8166667+00:00

Hi there,
I am trying to call Suggestions Rest Api in Angular and surprisingly, it throws a bad request error whenever I provide the search argument in its body instead of hard coding the value.
This is how the API's request looks:User's image

Result:
User's image

Now when I hard code the search parameter:
User's image

Results:
User's image

I would be highly grateful if someone can guide me through this error.
(Also, it is able to fetch the required data even with the failure)

Azure AI Search
Azure AI Search
An Azure search service with built-in artificial intelligence capabilities that enrich information to help identify and explore relevant content at scale.
1,027 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Dey, Nikita 20 Reputation points
    2023-11-21T07:11:45.3066667+00:00

    Hi, I was able to resolve the error.

    Make sure to handle the null values before calling the API. Since, the API would be called every time you enter a search query, I realized that the bad request error came in the beginning, when there was no search input and, after the search field was cleared (after backspace) to enter a new input.
    That is, the empty search field also revoked the API by sending query="".

    A simple if else statement worked for me!

    if (query != '' || null) {
            result = this.http
              .post<any>(
                ServerDetails.searchUriS,
                JSON.stringify({
                  search: `${query}`,
                  top: 5, // Change to a number
                  fuzzy: true,
    				....
    			}),
    			options )
    				
    

    Thanks for your support, I'll close the issue.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.