0

Trying to interact with the phaxio api using multipart form data. As per the request.js docs for making a post request I do

var request = require('request');

var options = {
    uri: 'https://api.phaxio.com/v1/send',
    headers: {'content-length':'0'}
};

var r = request.post(options);

var form = r.form();

form.append('api_key', '1234');
form.append('api_secret', '1234');
form.append('to', '1234');

r.on('response', function(chunk){
    console.log(chunk);
});

The response body I get from the r.on method is here http://pastie.org/private/wshbn9esendccrkoreeiow I'm unsure how I can see the api response body from the server after submitting the form data. Something like

{
    "success": true,
    "message": "Fax Sent!"
}
1
  • This is a shameless plug, but try running it through our tools. That will let you see what your client is generating and the server is responding with. Commented Sep 22, 2013 at 19:01

1 Answer 1

1

The method request.post() returns a readable stream. Just read the response:

var res = '';
r.on('data', function(data) {
  res += data;
});
r.on('end', function() {
  console.log(res);
});

You can also pipe the response to another writable stream:

var fs = require('fs');
var writable = fs.createWriteStream('/file');
r.pipe(writable);
Sign up to request clarification or add additional context in comments.

1 Comment

I thought 'response' was the response. Can you link me to more info on the different streams, 'response', 'data', 'end' and others i don't know about?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.