bigup401 has asked for the wisdom of the Perl Monks concerning the following question:

i need some help, am trying to code Alibaba cloud sms

#!/usr/bin/perl -w use LWP; use URI; use Net::SSL; my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 }, ); $ua->agent("MyApp/0.1"); my $url = "https://dysmsapi.aliyuncs.com"; my $params = [ RegionId => '1', PhoneNumbers => '4324234', SignName => + 'fghgfhg', TemplateCode => 'fghgfh']; my $uri = URI->new(); $uri->query_form(@$params); $urlparam = $uri->as_string; my $req = HTTP::Request->new(POST=>$url); $req->header('Content-Type' => 'application/json; charset=UTF-8'); $req->header('access_key_id' => 'sdsadasd'); $req->header('access_key_secret' => 'asdasdsa'); $req->header('api_version' => '2017-05-25'); $req->content($urlparam); print "Content-type: text/html\n\n"; my $res = $ua->request($req); if ($res->is_success) { print $res->content; } else { #print whole resluts from post print $res->content; print "\n"; #print error code print $res->status_line; }

am getting this error: FC41FAB2-2735-4495-A072-54850CD00A4Edysmsapi.aliyuncs.comInvalidVersionSpecified parameter Version is not valid. 400 Bad Request

am trying to make it like these example codes in php, python and ruby. because aliabab cloud sms doesn't have it in perl

PYTHON #!/usr/bin/env python #coding=utf-8 from aliyunsdkcore.client import AcsClient from aliyunsdkcore.request import CommonRequest client = AcsClient('<accessKeyId>', '<accessSecret>', 'default') request = CommonRequest() request.set_accept_format('json') request.set_domain('dysmsapi.aliyuncs.com') request.set_method('POST') request.set_protocol_type('https') # https | http request.set_version('2017-05-25') request.set_action_name('SendSms') request.add_query_param('RegionId', "default") request.add_query_param('PhoneNumbers', "7766") request.add_query_param('SignName', "iiuy") request.add_query_param('TemplateCode', "jkk") response = client.do_action(request) # python2: print(response) print(str(response, encoding = 'utf-8')) RUBY # gem install aliyunsdkcore require 'aliyunsdkcore' client = RPCClient.new( access_key_id: '<accessKeyId>', access_key_secret: '<accessSecret>', endpoint: 'https://dysmsapi.aliyuncs.com', api_version: '2017-05-25' ) response = client.request( action: 'SendSms', params: { "RegionId": "default", "PhoneNumbers": "7766", "SignName": "iiuy", "TemplateCode": "jkk" }, opts: { method: 'POST' } ) print response PHP <?php use AlibabaCloud\Client\AlibabaCloud; use AlibabaCloud\Client\Exception\ClientException; use AlibabaCloud\Client\Exception\ServerException; // Download&#65306;https://github.com/aliyun/openapi-sdk-php // Usage&#65306;https://github.com/aliyun/openapi-sdk-php/blob/master/ +README.md AlibabaCloud::accessKeyClient('<accessKeyId>', '<accessSecret>') ->regionId('cn-hangzhou') // replace regionId +as you need ->asDefaultClient(); try { $result = AlibabaCloud::rpc() ->product('Dysmsapi') // ->scheme('https') // https | http ->version('2017-05-25') ->action('SendSms') ->method('POST') ->host('dysmsapi.aliyuncs.com') ->options([ 'query' => [ 'RegionId' => "default", 'PhoneNumbers' => "7766", 'SignName' => "iiuy", 'TemplateCode' => "jkk", ], ]) ->request(); print_r($result->toArray()); } catch (ClientException $e) { echo $e->getErrorMessage() . PHP_EOL; } catch (ServerException $e) { echo $e->getErrorMessage() . PHP_EOL; }

Replies are listed 'Best First'.
Re: alibaba cloud sms api
by Corion (Patriarch) on Sep 02, 2019 at 11:29 UTC

    Have you compared the requests that PHP or Python send with what your Perl code sends?

    You can look at what Perl would send by using:

    print $req->as_string;

    Most likely, you are missing one or more of the parameters. Maybe the error message is somewhat misleading.

    Looking at the PHP source code, it sets version to 2016-09-27 here. So, have you made sure that the Ruby or PHP code actually works?

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: alibaba cloud sms api
by jcb (Parson) on Sep 03, 2019 at 00:52 UTC

    Do you have actual documentation for the API? What does the server expect to see where?

    Those examples are all using special libraries Alibaba provides for those languages, libraries which you will be effectively reimplementing in your Perl code. (Fear not — those libraries are also either very simple or overcomplicated messes to substitute for the lack of LWP.)

    The important part is that your code will look different from those examples because you are directly forming the request instead of using Alibaba's library.

    • Where do you send the POST request? (This will be a URL.)
    • What should be in that request? (What parameters?)
    • How should that information be encoded? (Does the API expect JSON? x-www-form-urlencoded? multipart/form-data? XML? something else?)