Perl WebService::Soundcloud - how to pass track parameters while uploading to Soundcloud
Asked Answered
C

1

6

I'm trying to upload a sound to soundcloud using WebService::Soundcloud. I've so far been able to make a couple of GET/POST requests following the examples provided by the WebService::Soundcloud documentation.

However, I can't find a decent example anywhere on how to do an upload while passing the required parameters i.e. track, and within it, asset_data, title e.t.c. I'm wondering if I should be sending out a multipart message.

Any examples will be appreciated!

Also, here's what I have tried so far: After authenticating and getting a valid WebService::Soundcloud instance.

GET my $response = $scloud->get( '/me/tracks' );
PUT my $response = $scloud->put( '/me/tracks/91576621', JSON::to_json({track=>{title=>"My test title",description=>"My test description"}}) );
POST my $file = '/home/ski/track1.mp3';
my $asset_data = File::Slurp::read_file( $file, binmode => ':raw' );
my $response = $scloud->post('/me/tracks', '{"track":{"title":"My test title","asset_data":"'.$asset_data.'"}}' );

This fails with "Request entity contains invalid byte sequence. Please transmit valid UTF-8"

Coprolalia answered 9/5, 2013 at 6:55 Comment(6)
Any code examples of what you've done so far (e.g. the GET/POST requests)?Hoke
After authenticating and getting a valid WebService::Soundcloud instance. GET my $response = $scloud->get( '/me/tracks' ); PUT my $response = $scloud->put( '/me/tracks/91576621', JSON::to_json({track=>{title=>"My test title",description=>"My test description"}}) ); POST my $file = '/home/ski/track1.mp3'; my $asset_data = File::Slurp::read_file( $file, binmode => ':raw' ); my $response = $scloud->post('/me/tracks', '{"track":{"title":"My test title","asset_data":"'.$asset_data.'"}}' ); This fails with "Request entity contains invalid byte sequence. Please transmit valid UTF-8"Coprolalia
Is that the full error message, or is there more to it? I don't know the Soundcloud API and have worked with neither WebService::Soundcloud nor JSON::to_json - but could it be that the problem is in the syntax for the JSON::to_json. Have you tried using encode_json() instead, as does the WebService::Soundcloud documentation for PUT requests?Hoke
@thunk That is all there is in $response->content(). And i did try encode_json() as well as just writing the json string myself. Thats probably not the issue since the PUT example above works just fine with JSON::to_json.Coprolalia
Well, that's all the ideas I had. I guess without time to spend on looking at that package, I can't help you any further. Let's hope somebody else who has used it before can help! Or otherwise, maybe try one of the languages where Souncloud provides an SDK?!?Hoke
Thanks for your responses! its gotta be in perl, i'm working on a much bigger project that is built entirely in perl.Coprolalia
X
0

The example you provide manually constructs the JSON but doesn't take into account the binary nature of the file content which means it's unlikely to work. It's also vulnerable to abusive content changing your JSON due to lack of escaping/proper encoding.

The documentation you cite has a put example which demonstrate the content needs to be encoded into JSON and then passed to the library. I've not used this api but it's probably a simple case of using encode_json as per the examples. I'll just show an example that's equivalent to your manual encoding:

use JSON qw(encode_json);

my $asset_data = "ascii, quotes (\"'), non-ascii: \000\001\002\003";

my $content =  encode_json({ track => { title => "My test title",
                                    asset_data => $asset_data}});

print $content . "\n";  ### inspection of encoding

And this shows that JSON uses a UTF-8 representation to deal with binary data:

{"track":{"asset_data":"ascii, quotes (\"'), non-ascii: \u0000\u0001\u0002\u0003","title":"My test title"}}

The key/values are being re-ordered there but it's equivalent JSON.

Xenia answered 22/8, 2018 at 18:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.