简体   繁体   English

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

[英]How to pass json to nodejs request post method?

I try to pass json to nodejs post method.But i'm getting error "The search query must be specified.". 我尝试将json传递给nodejs post方法。但出现错误“必须指定搜索查询”。 I have specified search string and filter the code. 我指定了搜索字符串并过滤了代码。 Can you advise what cause this error. 您能否告知导致此错误的原因。

Same request working in POSTMAN. 在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);

As per your postman screenshot you can try the below code by getting rid of qs: {query: 'nodejs*'} and adding the same inside the json . 按照您的postman屏幕截图,您可以通过摆脱qs: {query: 'nodejs*'}来尝试以下代码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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM