why I can't make search request to my Elastic Enterprise Search app using axios?

萝らか妹 提交于 2021-02-10 21:21:40

问题


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)
        response.status(200).send(data)

    } catch (error) {
        console.log(error)
        response.status(500).send(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": [
            null
        ],
        "transformResponse": [
            null
        ],
        "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

what went wrong in here ?


回答1:


The headers need to be passed as a named object in the config part.

So try it like this:

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

or even more concisely:

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

Tip: Postman is capable of pre-constructing axios requests when you click on Code. So next time you're not sure, Postman is here to help:



来源:https://stackoverflow.com/questions/64097968/why-i-cant-make-search-request-to-my-elastic-enterprise-search-app-using-axios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!