代码之家  ›  专栏  ›  技术社区  ›  abdoutelb

使用body-node js执行get请求

  •  0
  • abdoutelb  · 技术社区  · 7 年前

    我正在尝试执行弹性搜索API的GET请求 这个表格需要什么

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

    我用过 request 但我找不到如何将正文添加到请求中。

    有什么帮助吗?

    4 回复  |  直到 7 年前
        1
  •  2
  •   joaner    7 年前

    你可以看到关于 request(options, callback)

    也, GET 方法不应发送任何正文,请确认它不是 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
            }
        }
      }
    })
    
        2
  •  1
  •   hong4rc 6ark    7 年前

    如果你使用 GET ,您不能有正文,您只有查询。 您可以将查询转换为字符串并添加到URL,或者使用选项 qs 以下内容:

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

    如果要使用主体,应该使用 POST .

        3
  •  0
  •   t3__rry    7 年前

    嗨,你试过简单地把你的身体作为第二个论点来满足你的要求吗?

    request.get('yourEndpoint', { // your body })
    
        4
  •  0
  •   Ahmed Abdraboh    7 年前

    您不能向GET请求添加正文,您必须添加一个查询字符串,以便请求发送这些数据。