69

I'm using supertest to send get query string parameters, how can I do that?

I tried

var imsServer = supertest.agent("https://example.com");

imsServer.get("/")
  .send({
    username: username,
    password: password,
    client_id: 'Test1',
    scope: 'openid,TestID',
    response_type: 'token',
    redirect_uri: 'https://example.com/test.jsp'
  })
  .expect(200) 
  .end(function (err, res) {
    // HTTP status should be 200
    expect(res.status).to.be.equal(200);
    body = res.body;
    userId = body.userId;
    accessToken = body.access_token;
    done();
  });

but that didn't send the parameters username, password, client_id as query string to the endpoint. Is there a way to send query string parameters using supertest?

rastasheep
  • 10,416
  • 3
  • 27
  • 37
J K
  • 4,883
  • 6
  • 15
  • 24

2 Answers2

130

Although supertest is not that well documented, you can have a look into the tests/supertest.js.

There you have a test suite only for the query strings.

Something like:

request(app)
  .get('/')
  .query({ val: 'Test1' })
  .expect(200, function(err, res) {
    res.text.should.be.equal('Test1');
    done();
  });

Therefore:

.query({
  key1: value1,
  ...
  keyN: valueN
})

should work.

rastasheep
  • 10,416
  • 3
  • 27
  • 37
Robert T.
  • 1,620
  • 2
  • 13
  • 20
  • 6
    how silly that this simple function is not mentioned in the README. Makes me wonder if I am misunderstanding something about supertest. – vinhboy Mar 28 '17 at 21:59
  • 13
    @vinhboy Thats probably because supertest is just `superagent` with the added expect() function. If you look in superagent's docs https://visionmedia.github.io/superagent/ you can see the query function described with examples. – 8176135 May 13 '17 at 01:57
  • Update 2020, .query({k:v}) doesn't work with sails 1.0 and supertest 4.0.2. I had to ```.post('/users/data/?lang=in')``` to make it work – imsheth May 20 '20 at 08:51
  • 2
    @imsheth if you're using `post()`, then the corresponding function is `send()`, not `query()`. For example, try `.post('/users/data').send('lang=in')`. – nullromo Sep 02 '20 at 22:33
-1

it is not the best way but you also can do this:

await supertest(BASE_URL)
      .get(`/?search=${Title}&`)
Outsider
  • 9
  • 2