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

Dear Monks,
I am trying to understand how to create API for querying CJ affiliate network. However the question is probably more general. They suggest to use REST technology and gave an example:
sample URL: https://product-search.api.cj.com/v2/product-search?websit +e-id=12345678keywords=GPS&serviceable-area=US and Sample Header: https://product-search.api.cj.com/v2/product-search?website-id=1594990 +&keywords=%2Bsony+-camera GET /v2/product-search?website-id=1594990&keywords=%2Bsony+-camera HTT +P/1.1 Host: link-search.api.cj.com User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.8 +) Gecko/2009032609 Firefox/3.0.8 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0. +8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Cookie: __utma=128446558.2099392322683464700.1239639722.1239639722.123 +9927095.2; __utmz=128446558.1239639722.1.1.utmcsr=(direct)|utmccn=(di +rect)|utmcmd=(none); CONTID=8073; cjuMember=0; JSESSIONID=aM5RSWdqdd_ +5 Authorization: YOUR DEV KEY HERE HTTP/1.x 200 OK Server: Resin/2.1.17 Content-Type: application/xml Transfer-Encoding: chunked Date: Thu, 26 Apr 2009 10:25:03 GMT
What should I do to create API requests based on these requirements. I guess something like LWP, but I did not find how to insert all these fields there.
So please help.

Replies are listed 'Best First'.
Re: Creating REST API
by Your Mother (Archbishop) on Sep 09, 2010 at 21:37 UTC

    You're lucky this is one of my favorite general problems. :) Here is a functional proof of concept for you-

    use warnings; use strict; use XML::LibXML; use WWW::Mechanize; use URI; my $api = URI->new("https://product-search.api.cj.com/v2/product-searc +h"); my %constants = ( "website-id" => "1594990" ); my $client = WWW::Mechanize->new( agent => "MyBot/0.0000001", autocheck => 0 ); my $api_call = $api->clone; # To make $api reusable without polluting +it. $api_call->query_form( %constants, keywords => "+sony -camera" ); $client->add_header(Authorization => "Some Nonsense Key 1234567890"); $client->get($api_call); my $dom = XML::LibXML->new->parse_string($client->content); print $dom->serialize(1); __END__ <?xml version="1.0" encoding="UTF-8"?> <cj-api> <error-message>Not Authenticated: Some Nonsense Key 1234567890</erro +r-message> </cj-api>
      Thanks a lot !!! Looking forward to check it.
Re: Creating REST API
by Your Mother (Archbishop) on Sep 09, 2010 at 18:49 UTC

    Don't have time to elaborate but you should check out URI::Amazon::APA (read the source code!). It is a URI interface to some of Amazon's services and it's really simple and powerful at the same time. It seems like a good match for what you're doing. Making the request (LWP... or WWW::Mechanize would be fine) and parsing the responses is up to you still, of course. If their reply data format is sane it won't be hard. Good luck.

Re: Creating REST API
by BrowserUk (Patriarch) on Sep 09, 2010 at 21:13 UTC

    Take a look at LWP::UserAgent. It allows you to add custiom headers, handles cookies etc.

Re: Creating REST API
by ikegami (Patriarch) on Sep 09, 2010 at 19:48 UTC
    If I understand correct, you're asking for help building the url. You can use URI. (See query_form.)
      I looked at URI query_form you suggested. Does it mean that it allows me to pass all header parameters I listed in the example?

        Any reasonable agent (LWP::UserAgent or WWW::Mechanize are good choices) is going to handle the headers for you and the only thing special about them seems to be the dev token and maybe but not necesisarily the cookie. Without a spec I can't offer more but you should be able to add the dev token this way-

        my $mech = WWW::Mechanize->new(); ... $mech->add_header(Authorization => "YOUR DEV KEY HERE"); ...

        And you might have to also do a sign-in / authentication before doing your API calls to get an auth'd session cookie, but maybe not. Then you robustly and easily build API calls with URI or a subclass of it.