0

I'm trying to turn this CURL call into request.js

curl -i -X POST "https://api.soundcloud.com/tracks.json" \
            -F 'oauth_token=***' \
            -F 'track[asset_data][email protected]' \
            -F 'track[title]=Track title' \
            -F 'track[sharing]=public'

The interesting bit is that the file attachment ('asset_data') is on the 2nd level of the formData object.

THIS is not working:

var formData = {
  oauth_token: '***',
  track: {
    asset_data: fs.createReadStream('music.wav'),
    title: 'Track title',
    sharing: 'public'
  }

}
request.post({url:'http://service.com/upload', formData: formData}...

I know it's because the docs and this post say I have to embed the track inside of JSON.stringify(track)

but when I do that and the POST executes it comes back with "buffer":[],"length":0, I get the sense that it didn't wait for the file to stream before it stringify-ed it.

here's all the code in one place:

function postToSoundcloud(){
    var track = {'sharing': 'public',
                 'title': TRACK_NAME,
                 'asset_data': fs.createReadStream('music.wav')
                }
    var formData = {
        oauth_token: '***',
        track: JSON.stringify(track)
    };
    var req = request.post({url:'https://api.soundcloud.com/tracks.json', 'formData': formData}, function optionalCallback(err, httpResponse, body) {
        if (err) {
            return console.error('upload failed:', err);
        }
        console.log('Upload successful!  Server responded with:', body);
    }); 
}

If there are better approaches instead of using request.js I'm open to it. I can tell request.js is very popular, but also I'm very new to node.

thank you all very much

2
  • you want your post request in synchronous manner ? Commented Apr 18, 2016 at 6:02
  • @Sagar yes, in this case things are synchronous Commented Apr 18, 2016 at 13:49

1 Answer 1

1

You need to format it just like you do for curl:

var formData = {
  oauth_token: '***',
  'track[asset_data]': fs.createReadStream('music.wav'),
  'track[title]': 'Track title',
  'track[sharing]': 'public'
}
Sign up to request clarification or add additional context in comments.

Comments

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.