title: Upload a track to SoundCloud with post request from Node.js to SC API
body: <p>This is my first question on Stackoverflow so let me apologize in advance for any faux pas. (I'd be grateful for any comments about how I could improve this question.) Also, thanks so much for taking a look!</p>
  
  <p>For the last couple of days I've been trying to upload a song to SoundCloud from Node.js with no success. </p>
  
  <p>The really frustrating part is that I AM able to make the request from the front-end using the SoundCloud JavaScript SDK. (The only reason I'm making the post from the backend is because I ultimately need to be able to schedule the 'POST' request to be made at a later date from the server).</p>
  
  <p>I'm starting to wonder if it's even possible to upload a song with a POST request via the API. I suspect that it might only be possible via the SDK, which, as best as I can tell, isn't available in Node.</p>
  
  <p>For one thing, in the API documentation, it doesn't list 'POST' as an available method on the <a href="https://developers.soundcloud.com/docs/api/reference#users" rel="nofollow noreferrer">'/track' route</a>. This can be found in the 'Subresources' table at the bottom of the '/tracks' section. (On the other hand, I'm wondering if it's possible to upload a song with a PUT request. I thought PUT was only for updates, but while reading some AWS docs, I noticed that objects can be uploaded to an S3 bucket with a POST request from the client or a PUT request from the server. This would still be consistent with the SC docs since they suggest using a POST with the SDK from the front-end, but don't list POST as an option in the API.)</p>
  
  <p>However in the <a href="https://developers.soundcloud.com/docs/api/guide#uploading-files" rel="nofollow noreferrer">SDK documentation</a> it says:</p>
  
  <blockquote>
    <p>To upload a sound, send a POST request to the /tracks endpoint. This
    is done with one of our SDKs using the post method and passing
    information about the track being uploaded.</p>
  </blockquote>
  
  <p>I tried utilizing the node-soundcloud and the soundclouder npm packages with no success. I'm able to authenticate the user and make a GET request to the '/me' route (which, I'm almost certain I shouldn't be able to do without authenticating). </p>
  
  <p>The following example is with node-soundcloud, but soundclouder is nearly identical. </p>
  
  <pre><code>    SC.init({
          id: client_id, 
          secret: client_secret, 
          uri: redirect_uri,
          accessToken: body.fields.accessToken
      });
      SC.get('/me/activities', function(err, data){
          if ( err ) {
              throw err;
          } else {
              console.log("data", data);
              res.end();
          }
      })
  </code></pre>
  
  <p>However, I'm not able to make a 'POST' request to the '/tracks' route. </p>
  
  <pre><code>    SC.post('/tracks', {
          'title': 'test title',
          'asset_data': body.file.buffer
      }, function(err, data){
          console.log("data", data);
          res.end();
      })
  </code></pre>
  
  <p>This returns </p>
  
  <pre><code>{ errors: [ { error_message: '401 - Unauthorized' } ] }
  </code></pre>
  
  <p>I've opened 'issues' via both of their Githubs. (Node-soundcloud has actually been really helpful in trying to resolve this.)</p>
  
  <p>My most recent attempt has been to try to imitate the successful front-end attempt by constructing an HTTP request in Postman. Even this isn't working. In Postman I'm getting this error: "Request entity contains invalid byte sequence. Please transmit valid UTF-8."</p>
  
  <p>I get the same error, when I try to make the request from Node.js using the 'request' npm library.</p>
  
  <p>Here's data from the successful request from the front-end that I pulled out of the Network tab in chrome dev tools. (Realizing that you can convert a request into a curl post from dev tools was pretty great!):</p>
  
  <pre><code>General
  Headers
  Request URL:https://api.soundcloud.com/tracks?
  Request Method:POST
  Status Code:201 Created
  Remote Address:72.21.91.127:443
  
  Request Headers
  POST /tracks? HTTP/1.1
  Host: api.soundcloud.com
  Connection: keep-alive
  Content-Length: 73945
  Pragma: no-cache
  Cache-Control: no-cache
  Origin: http://localhost:1337
  User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36
  Content-Type: multipart/form-data; boundary=----WebKitFormBoundary6zA9wPyul1LgLYTW
  Accept: */*
  Referer: http://localhost:1337/
  Accept-Encoding: gzip, deflate
  Accept-Language: en-US,en;q=0.8
  
  Request Payload
  ------WebKitFormBoundary6zA9wPyul1LgLYTW
  Content-Disposition: form-data; name="track[asset_data]"; filename="test2.mp3"
  Content-Type: audio/mp3
  
  
  ------WebKitFormBoundary6zA9wPyul1LgLYTW
  Content-Disposition: form-data; name="track[title]"
  
  6:14
  ------WebKitFormBoundary6zA9wPyul1LgLYTW
  Content-Disposition: form-data; name="format"
  
  json
  ------WebKitFormBoundary6zA9wPyul1LgLYTW
  Content-Disposition: form-data; name="oauth_token"
  
  (THIS IS WHERE THE ACCESS TOKEN GOES)
  ------WebKitFormBoundary6zA9wPyul1LgLYTW--
  </code></pre>
  
  <p>Finally, I found this <a href="https://stackoverflow.com/questions/32159979/soundcloud-http-api-post-tracks-422-unprocessable">other question on Stackoverflow</a> which seems to suggest that I might need to "frame" the track as a "recording" made on Soundcloud.</p>
  
  <p>Please let me know if there's any additional information that would be helpful for me to upload. Thanks very much in advance for your help!</p>