Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9244859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:08:05+00:00 2026-06-18T09:08:05+00:00

In node.js, I have trouble making superagent and nock work together. If I use

  • 0

In node.js, I have trouble making superagent and nock work together. If I use request instead of superagent, it works perfectly.

Here is a simple example where superagent fails to report the mocked data:

var agent = require('superagent');
var nock = require('nock');

nock('http://thefabric.com')
  .get('/testapi.html')
  .reply(200, {yes: 'it works !'});

agent
  .get('http://thefabric.com/testapi.html')
  .end(function(res){
    console.log(res.text);
  });

the res object has no ‘text’ property. Something went wrong.

Now if I do the same thing using request:

var request = require('request');
var nock = require('nock');

nock('http://thefabric.com')
  .get('/testapi.html')
  .reply(200, {yes: 'it works !'});

request('http://thefabric.com/testapi.html', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body)
  }
})

The mocked content is displayed correctly.

We used superagent in the tests so I’d rather stick with it. Does anyone know how to make it work ?

Thank’s a lot,
Xavier

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:08:06+00:00Added an answer on June 18, 2026 at 9:08 am

    My presumption is that Nock is responding with application/json as the mime type since you’re responding with {yes: 'it works'}. Look at res.body in Superagent. If this doesn’t work, let me know and I’ll take a closer look.

    Edit:

    Try this:

    var agent = require('superagent');
    var nock = require('nock');
    
    nock('http://localhost')
    .get('/testapi.html')
    .reply(200, {yes: 'it works !'}, {'Content-Type': 'application/json'}); //<-- notice the mime type?
    
    agent
    .get('http://localhost/testapi.html')
    .end(function(res){
      console.log(res.text) //can use res.body if you wish
    });
    

    or…

    var agent = require('superagent');
    var nock = require('nock');
    
    nock('http://localhost')
    .get('/testapi.html')
    .reply(200, {yes: 'it works !'});
    
    agent
    .get('http://localhost/testapi.html')
    .buffer() //<--- notice the buffering call?
    .end(function(res){
      console.log(res.text)
    });
    

    Either one works now. Here’s what I believe is going on. nock is not setting a mime type, and the default is assumed. I assume the default is application/octet-stream. If that’s the case, superagent then does not buffer the response to conserve memory. You must force it to buffer it. That’s why if you specify a mime type, which your HTTP service should anyways, superagent knows what to do with application/json and why if you can use either res.text or res.body (parsed JSON).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have been facing serious trouble in making work cucumber-js in windows. i start
I am having some trouble getting OpenCover to work I have the following folder
I have some trouble with selecting values in node set. I have a string
I have a trouble getting the following result from a xml/xslt transformation: <root> <node>
I have a trouble on appending new node to xmldocument (created in the memory).
I have some trouble to convert some variable from void* to MPI_Aint. Here is
I am facing trouble with Node.getTextContent() of org.w3c.dom. I have following code block: String
I'm having some trouble determining the correct context for a node set. I have
I am using the Node.js driver for MongoDB and have no trouble constructing query
I have a binary tree where each node can have a value. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.