javascript - 如何将json传递给nodejs请求post方法?

标签 javascript node.js node-modules npm-request

我尝试将 json 传递给 nodejs post 方法。但我收到错误“必须指定搜索查询。”。我已经指定了搜索字符串并过滤了代码。您能否告知导致此错误的原因。

同样的请求在 POSTMAN 中工作。

//Load the request module
var express = require('express');
var request = require('request');
var app = express();

//Lets configure and request
request({
    url: 'http://host:8080/rest/1.0/search', 
    qs: {query: 'nodejs*'}, 
    method: 'POST',
    json: {
        filter:
        {
            community: ['33862a97-44e5-4de5-9206-db0a61dd83ca'],
            vocabulary: ['b80f3576-0642-4049-bb07-d72a0dd9e3e0','48029bb8-0585-4ed5-afaa-55014aebfcb3'],
            type: {asset:['00000000-0000-0000-0000-000000011001']},
        },
        fields: ['name']

    }
}, function(error, response, body){
    if(error) {
        console.log(error);
    } else {
        console.log(response.statusCode, body);
}
});
app.listen(8080);

最佳答案

根据您的postman屏幕截图您可以通过删除 qs: {query: 'nodejs*'} 来尝试下面的代码并在json内添加相同的内容.

//Load the request module
var express = require('express');
var request = require('request');
var app = express();

//Lets configure and request
request({
    url: 'http://host:8080/rest/1.0/search', 
    method: 'POST',
    json: {
        query: 'nodejs*',
        filter:
        {
            community: ['33862a97-44e5-4de5-9206-db0a61dd83ca'],
            vocabulary: ['b80f3576-0642-4049-bb07-d72a0dd9e3e0','48029bb8-0585-4ed5-afaa-55014aebfcb3'],
            type: {asset:['00000000-0000-0000-0000-000000011001']},
        },
        fields: ['name']

    }
}, function(error, response, body){
    if(error) {
        console.log(error);
    } else {
        console.log(response.statusCode, body);
}
});
app.listen(8080);

关于javascript - 如何将json传递给nodejs请求post方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40598049/

相关文章:

javascript - 在 IOS 应用 Webview 中运行 Angular 应用

javascript - 使用 Javascript 通过 AJAX 数据动态生成表

javascript - 用于多个 HTML 内容的更简单的 Javascript

javascript - 在 vue.js 组件模态窗口中传递变量

javascript - 未处理的拒绝错误 : WHERE parameter “number_phone” has invalid “undefined” value

reactjs - 使用 create-react-native-app 安装 react-native 会出错

javascript - Cypress 可以运行从 Cypress 文件夹外部导入的测试文件吗?

node.js - 无法在 Ubuntu 16.04 服务器上安装 npm

javascript - 回发后在 TextBox 中设置焦点

node.js - meteorhacks :npm Meteor. npmRequire 在按照说明操作时不起作用