question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No match for request POST even nock successfully matched request

See original GitHub issue

How real request data looks like:

{ 
  port: 443,
  path: '/',
  host: 'dynamodb.us-east-1.amazonaws.com:443',
  hostname: 'dynamodb.us-east-1.amazonaws.com',
  method: 'POST',
  headers:  {
     'user-agent': 'aws-sdk-nodejs/2.2.48 darwin/v4.4.3',
     'content-type': 'application/x-amz-json-1.0',
     'x-amz-target': 'DynamoDB_20120810.ListTables',
     'x-amz-content-sha256': '...',
     'content-length': 2,
     host: 'dynamodb.us-east-1.amazonaws.com',
     'x-amz-date': '...',
     authorization: '…'
   },
  body: '{}' 
}

How im trying to match this request with nock:

nock('https://dynamodb.us-east-1.amazonaws.com', {
  reqheaders: {
    'x-amz-target': 'DynamoDB_20120810.ListTables'
  }
})
.post('/', {})
.reply(503, 'The service is currently unavailable or busy.')
.log((data) => console.log(data))

From log statement, i can see that nock is able to match this request: matching https://dynamodb.us-east-1.amazonaws.com:443 to POST https://dynamodb.us-east-1.amazonaws.com:443/: true, but im not receiving 503, 'The service is currently unavailable or busy.', but error: 'Nock: No match for request POST https://dynamodb.us-east-1.amazonaws.com/ {}'

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:19 (4 by maintainers)

github_iconTop GitHub Comments

29reactions
ksmith97commented, Jun 6, 2016

I think I ran into a similar issue. Its not clear from the documentation how to use nock when you specify the entire url in the initial nock params(In my case because the URL comes from env). If you pass ‘/’ into the post it wont match properly(first thing I tried as well) and it also fails if you dont pass anything. I found it only worked after passing a empty string.

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

// Errors out
nock('http://localhost/path').post().reply(200);

request.post('http://localhost/path', function(err, body) {
  console.log(err, body);
});

//Also errors out
nock('http://localhost/path').post('/').reply(200);

request.post('http://localhost/path', function(err, body) {
  console.log(err, body);
});

//Works as intended.
nock('http://localhost/path').post('').reply(200);

request.post('http://localhost/path', function(err, body) {
  console.log(err, body);
});

Maybe there could be a doc update? Or maybe its specified somewhere and I just missed it…

21reactions
sunil-lullacommented, Jul 30, 2018

Though it’s very late to reply on this but it will surely help the new explorers.

Just use persist() for this, as the docs say

When you setup an interceptor for a URL and that interceptor is used, it is removed from the interceptor list. This means that you can intercept 2 or more calls to the same URL and return different things on each of them. It also means that you must setup one interceptor for each request you are going to have, otherwise nock will throw an error because that URL was not present in the interceptor list. If you don’t want interceptors to be removed as they are used, you can use the .persist() method.

nock("https://abc.co")
    .persist()
    .get("/getresponse")
  .delayBody(1000)
  .reply(200, {
   name:"Sunil Lulla",
   "jobRole":"SoftwareDeveloper"
  });

Hope it will help you 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: Nock: No match for request - Stack Overflow
I am calling 2 apis, 1 is post and 1 is get. And doing Promise.all([1stApi, 2edApi]). But getting error as - Error: Nock:...
Read more >
Nock Headers Error: Nock: No Match For Request - ADocLib
Solvednock No match for request POST even nock successfully matched request for each request you are going to have otherwise nock will throw...
Read more >
No match error when testing an Express.js controller doing ...
I had a controller requesting the same backend service but onto 2 different endpoints. My intent was to mock these calls using the...
Read more >
Network Requests - Cypress Documentation
Additionally you can even stub and mock a request's response. ... No guarantee your stubbed responses match the actual data the server sends;...
Read more >
Nock: HTTP Mocking and Expectations - Morioh
nock ('http://www.example.com') .post('/user', _.matches({ address: { country: 'US' } ... If no request headers are specified for mocking then Nock will ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found