为什么我无法使用axios向我的Elastic Enterprise Search应用发出搜索请求? [英] why I can't make search request to my Elastic Enterprise Search app using axios?

查看:42
本文介绍了为什么我无法使用axios向我的Elastic Enterprise 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)
        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 ?

解决方案

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:

这篇关于为什么我无法使用axios向我的Elastic Enterprise Search应用发出搜索请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆