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

hey guys, I am trying to use Bamboo's rest api and I need to post an xml formatted payload to it. I have been able to do other gets and posts without a payload, but I am having trouble sending a payload that bamboo recognizes. In bamboos rest api documentation: (https://docs.atlassian.com/bamboo/REST/4.0/), it says that I need to send the following as the payload:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <label name="someLabel"/>
here is my test code that I am trying to use:
use LWP::UserAgent; my $urlRequest = "http://bamboohost/rest/api/latest/result/projectname +-planname-latest/label"; my $message = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"y +es\"?><label name=\"testlabel\"/>"; my $ua = LWP::UserAgent->new; my $req = HTTP::Request->new( POST => "$urlRequest"); $req->authorization_basic("username", "password"); $req->content($message); my $result = $ua->request( $req );
when I run this, I get the following error message:
The server refused this request because the request entity is in a for +mat not supported by the requested resource for the requested method.
am I sending the xml payload properly? what am I missing here?


UPDATE: I was able to get it to work. This is the solution:
use LWP::UserAgent; my $urlRequest = "http://bamboohost/rest/api/latest/result/projectname +-planname-latest/label"; my $message = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"y +es\"?><label name=\"testlabel\"/>"; my $ua = LWP::UserAgent->new; my $req = HTTP::Request->new( POST => "$urlRequest"); $req->authorization_basic("username", "password"); $req->content($message); $req->content_type('application/xml'); my $result = $ua->request( $req );

thanks,

Replies are listed 'Best First'.
Re: Need help with sending xml formatted payload using HTTP::Request
by Your Mother (Archbishop) on Jan 20, 2015 at 18:47 UTC

    Not sure this will be of any use since I can’t test but it does have the virtue of declaring its payload type and that sounds related; Mech is easier to work with than plain UA too. I added the query param for the auth type too in case it matters. You’ll have to tweak the user/pass.

    use strictures; use WWW::Mechanize; use URI; use MIME::Base64; my $endpoint = URI->new("http://bamboohost/rest/api/latest/result/proj +ectname-planname-latest/label"); $endpoint->query_form( os_authType => "basic" ); # In case anonymonk i +s right. my $xml = <<''; <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <label name="testlabel"/> my $mech = WWW::Mechanize->new( autocheck => 0 ); $mech->default_header( Authorization => 'Basic ' . encode_base64( USER + . ':' . PASSWORD ) ); my $response = $mech->post( $endpoint, Content_Type => "application/xml", Content => $xml ); print $response->as_string;

    Please don’t use basic auth in the wild without https.

    (Update: fixed a couple code oversights but this is still probably not working code. Follow-up pending.)

Re: Need help with sending xml formatted payload using HTTP::Request
by NetWallah (Canon) on Jan 20, 2015 at 22:45 UTC
    Try adding
    $req->content_type('text/xml');
    before $req->content(...

    This should get you close to Your Mother's WWW::Mech-based implementation.

            "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

      I have tried that, but I get the same error.
        Hmm .. try adding :
        $req->header('Accept' => 'application/xml');
        since, by default, apparently the accept list is empty, and altassian REST used by bamboo seems to require it.

                "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

Re: Need help with sending xml formatted payload using HTTP::Request
by Your Mother (Archbishop) on Jan 20, 2015 at 18:21 UTC

    Don’t know if your example is what you’re really sending but the XML you show is not well formed so you’re not actually sending XML. Could be the problem.

      right, I mistyped that in this post, but it is correct in my code. I have corrected this post. the problem still remains :(
Re: Need help with sending xml formatted payload using HTTP::Request
by Anonymous Monk on Jan 20, 2015 at 18:37 UTC
    I don't know what this Bamboo stuff is but it says
    Use HTTP basic authentication (Authorisation HTTP header) containing 'Basic username:password'. The URL must also contain the 'os_authType=basic' query parameter.
    And gives an example
    http://host:8085/rest/api/latest/plan?os_authType=basic
    Seems to me you're having problems with this Bamboo thing rather then Perl?
      this is not an authentication issue. I am able to make other GET and POST calls. The issue I am having is trying to send an xml payload.