Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursive calls Examples #42

Open
oscmejia opened this issue Oct 8, 2012 · 8 comments
Open

Recursive calls Examples #42

oscmejia opened this issue Oct 8, 2012 · 8 comments

Comments

@oscmejia
Copy link

oscmejia commented Oct 8, 2012

Can you provide an example of recursive calls. I'm interested in the case where you, let's say authenticate a call to an API and then use the token returned for future calls.

Thanks!

@ajostergaard
Copy link

+1

2 similar comments
@davistan
Copy link

davistan commented Jan 2, 2013

👍

@gerardobort
Copy link

👍

@joehler
Copy link

joehler commented Apr 16, 2013

I've run into this also. My solution for the token was to get the token and then use the 'before' function to set the token in subsequent outgoing requests.

.post(authUrl, postObject)
  .expect(200)
  .expect('should respond with the authorize token', function (err, res, body) {
    var result = JSON.parse(body);
    assert.isNotNull(result.access_token);

    // Before each subsequent call, set the authorization header
    suite.before('setAuth', function (outgoing) {
      outgoing.headers['Authorization'] = result.access_token;
      return outgoing;
    });
   }) 

@gerardobort
Copy link

thanks @joehler !! that helped!... the only thing is that when running with --spec param, the api-easy log is misleading because I'm modifying the outgoing.uri and it shows the old one.

@exocom
Copy link

exocom commented Feb 9, 2014

I'm having a similar issue, I can't figure out how to pass a value from the previous call to the path of the next call.

I want to POST an item them I want to do a DELETE with the id that was returned from the POST.

var partId = null;

.post('/parts', {name:'foo'})
  .expect(200)
  .expect('should respond with 1 part', function (err, res, body) {
    var result = JSON.parse(body);

    assert.equal(result.name, 'foo');
    assert.equal(typeof result.id === 'string',true);

    partId = result.id;
   })

.then()

.del('/parts/' + partId)
  .expect(200)

What am I doing wrong?

@stevendearborn
Copy link

It seems that the suite.del has already been initialized the the null value of the partId already and no further modification is do with it by the time it executes.

The only way is to add this to the end of subsequent sequential calls (i.e. using next() ) by using suite.before to append the partId to the URL passed implicitly to suite.del.

I am trying this now, since this would be a very common pattern, but I also need to restore/remove the appended id when I am done.

@piercus
Copy link

piercus commented Dec 14, 2016

this is a trick, but i'm posting anyway, in case someone has same issue :

var request = require("request");

...

.del(function(outgoing, callback){
     outgoing.uri = outgoing.uri+'parts/' + partId
     request(outgoing, callback)
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants