Why I can't make simple search request to my Elastic App Search?

so I want to make search request to my elastic enterprise search app using guide from the documentation in here , I don't want to use elastic node JS client, but I want to make http request using axios.

here is the code I use

        const url = "https://XXXXXXX38ce49e5aff1aa238e6f9195.ent-search.asia-southeast1.gcp.elastic-cloud.com/api/as/v1/engines/events/search"
        const headers = {"Authorization": "Bearer search-qpz4bu5o7ubb8j31r15juyrh"}
        const jsonData = {
            query: "hello there"
        try {
            const {data} = await axios.post(url,jsonData,headers)
        } catch (error) {

but I always get 401 error like this:

        "message": "Request failed with status code 401",
        "name": "Error",
        "stack": "Error: Request failed with status code 401\n    at createError (/Users/xxx/Documents/elastic_jxxx/firebase_emulator/functions/node_modules/axios/lib/core/createError.js:16:15)\n    at settle (/Users/xxxx/Documents/elastic_jakarta_kumpul_muslim/firebase_emulator/functions/node_modules/axios/lib/core/settle.js:17:12)\n    at IncomingMessage.handleStreamEnd (/Users/xxxxx/Documents/elastic_xxxxx/firebase_emulator/functions/node_modules/axios/lib/adapters/http.js:244:11)\n    at IncomingMessage.emit (events.js:203:15)\n    at endReadableNT (_stream_readable.js:1145:12)\n    at process._tickCallback (internal/process/next_tick.js:63:19)",
        "config": {
            "url": "https://XXXXXXXa638ce49e5aff1aa238e6f9195.ent-search.asia-southeast1.gcp.elastic-cloud.com/api/as/v1/engines/events/search",
            "method": "post",
            "data": "{\"query\":\"hello there\"}",
            "headers": {
                "Accept": "application/json, text/plain, */*",
                "Content-Type": "application/json;charset=utf-8",
                "User-Agent": "axios/0.20.0",
                "Content-Length": 28
            "transformRequest": [
            "transformResponse": [
            "timeout": 0,
            "xsrfCookieName": "XSRF-TOKEN",
            "xsrfHeaderName": "X-XSRF-TOKEN",
            "maxContentLength": -1,
            "maxBodyLength": -1,
            "Authorization": "Bearer search-qpz4bu5o7ubb8j31r15juyrh"

I believe I have put the correct search key, I can get the sucessful response using the same baseURL and search key in postman like this

could you please me ?

Based on axios' documentation, I think it's your headers config that needs fixing. Try this:

const { data } = await axios.post(url, jsonData, { headers });

The issue is that your Authorization header needs to be nested under the headers key - the 3rd param/arg is for general configuration, not just headers specifically. Hope that makes sense :slight_smile:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.