0

I try to pass json to nodejs post method.But i'm getting error "The search query must be specified.". I have specified search string and filter the code. Can you advise what cause this error.

Same request working in 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);
user2848031
  • 187
  • 12
  • 36
  • 69
  • `request()` is posting the data to `/rest/1.0/search?query=nodejs%2A`. Is this the expected path or should the query be passed in the `json` field? This is unrelated to your issue, but there is no need to set the `headers`, adding the `json` field will automatically add the `"Content-type: application/json"` header for you. – Dan Nagle Nov 14 '16 at 22:03
  • query should be passed in the json field. Let me attach postman screen short here. – user2848031 Nov 15 '16 at 00:10

1 Answers1

1

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.

//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);
Aruna
  • 11,959
  • 3
  • 28
  • 42