最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Perform Get request with body - Node js - Stack Overflow

programmeradmin2浏览0评论

I'm trying to perform a GET request to elastic search api which is needed in this form

GET /_search
{
    "query": {
        "more_like_this" : {
            "fields" : ["title", "description"],
            "like" : "Once upon a time",
            "min_term_freq" : 1,
            "max_query_terms" : 12
        }
    }
}

I used request But I can't find how to add body to the request.

Any help?

I'm trying to perform a GET request to elastic search api which is needed in this form

GET /_search
{
    "query": {
        "more_like_this" : {
            "fields" : ["title", "description"],
            "like" : "Once upon a time",
            "min_term_freq" : 1,
            "max_query_terms" : 12
        }
    }
}

I used request But I can't find how to add body to the request.

Any help?

Share Improve this question edited Jun 25, 2018 at 10:21 LucasSeveryn 6,3028 gold badges41 silver badges66 bronze badges asked Jun 25, 2018 at 9:56 abdoutelbabdoutelb 1,0531 gold badge15 silver badges34 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

You can see the document about request(options, callback)

Also, GET method should't send any body, please confirm it's not POST.

request.get('http://localhost:8092/_search', {
  json: true,
  body: {
    "query": {
        "more_like_this" : {
            "fields" : ["title", "description"],
            "like" : "Once upon a time",
            "min_term_freq" : 1,
            "max_query_terms" : 12
        }
    }
  }
})

If you use GET, you can't have body, you just have query. You can convert your query to string and add to your url, or use option with qs:

option = {
    url: 'your_url',
    qs: your_query
};
request(option, (error,res)=>{});

If want use body, you should use POST.

You can not add body to a get request, you will have to add a query string for the request to send those data.

发布评论

评论列表(0)

  1. 暂无评论