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

How to pass json to nodejs request post method?

我尝试将 json 传递给 nodejs post method.But 我遇到错误 "The search query must be specified."。我已指定搜索字符串并过滤代码。您能否告知导致此错误的原因。

同样的请求在 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);