简体   繁体   中英

curl post request jquery equivalent

I want to use echoprint - http://echoprint.me/start - to send an mp3 file locally from my computer in a post request in order to retrieve this song's details from their server. I am trying to write this post request in jquery in my text editor to retrieve a json object with the song details so I can view this in my browser's console, and then use the data from there. Their website explains how to make this post request by using curl for which the following code works in the command line. This returns a json object, however this is in the terminal so isn't much use to me as far as I know.

curl -F "api_key=################" -F "filetype=mp3" -F "track=@audio.mp3" "http://developer.echonest.com/api/v4/track/upload"

I want to make this request using jquery in my text editor so I can make the same request and retrieve the json data in the browser's console.

I have tried this code but unfortunately it doesn't work

   $.post("http://developer.echonest.com/api/v4/track/upload?api_key=############&url=http://tylergrund.com/mp3/MP3s%20from%20home/Coldplay/01%20Speed%20Of%20Sound.mp3" )
  .done(function( data ) {
    console.log( data );
  });

Can anyone provide the appropriate code. There are instructions here http://developer.echonest.com/docs/v4/track.html but they only explain how to do this using curl. Any help would be greatly appreciated. I am a beginner to this so please go easy.

You need to POST the parameters instead of adding them to the URL causing a GET request:

$.post("http://developer.echonest.com/api/v4/track/upload", {  
          "api_key":"############",
          "url":"http://tylergrund.com/mp3/MP3s%20from%20home/Coldplay/01%20Speed%20Of%20Sound.mp3" 
      },
      function( data ) {
                console.log( data );
      },
      "JSON" );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM