in reply to Changing data in Sharepointlist via LWP/JSON - OData.ODataContentTypeException :(

I just looked at your code again, and I find it a very weird approach to (re)set $ua->default_header for each request. The more traditional way would be to simply construct the appropriate HTTP::Request objects instead. At least from my spurious attempts, it does not feel as if a POST request uses the ->default_header:

> perl -MLWP::UserAgent -e "$ua=LWP::UserAgent->new();$ua->default_hea +der('Content-type' => 'application/json;odata=verbose'); $ua->add_handler('request_send',sub{shift->dump;return}); $ua->post('http://localhost:1234',{foo => bar})" POST http://localhost:1234 User-Agent: libwww-perl/6.04 Content-Length: 7 Content-Type: application/x-www-form-urlencoded foo=bar

Explicitly creating the HTTP::Request and then sending that one seems to create headers that I expect:

>perl -MLWP::UserAgent -MHTTP::Request::Common -e "$ua=LWP::UserAgent- +>new(); req= POST 'http://localhost:1234', 'Content-type' => 'applicati +on/json;odata=verbose', Content => { foo => 'bar' }; $ua->add_handler('request_send',sub{shift->dump;return}); $ua->req +uest($req)" POST http://localhost:1234 User-Agent: libwww-perl/6.04 Content-Length: 7 Content-Type: application/json;odata=verbose foo=bar

So I suggest you do more debugging on whether the values you think you are sending actually get send, and you also switch to the more common and accepted use of ->default_header() of setting only default headers and not per-request specific headers.

Replies are listed 'Best First'.
Re^2: Changing data in Sharepointlist via LWP/JSON - OData.ODataContentTypeException :(
by timb (Novice) on May 16, 2014 at 08:08 UTC

    You are absolutely right! I was using LWP the wrong way. Quite embarassing after using it for so many years. But so far I never had the need to send a different content-type.

    As soon as I corrected my code I got a completely new error ;)

    But this one is Sharepoint-related so I won't bother anyone here anymore.

    Thanks for helping me out!

    tiMb

      Just for completeness. I now have a working example of how to change data in Sharepoint via Perl.
      #!/usr/bin/perl use strict; use LWP::UserAgent; use LWP::Authen::Ntlm; use HTTP::Request::Common; use JSON; use Data::Dumper; # config my $host = "mysite.local"; my $user = 'domain\spadmin'; my $pass = "iLikeMyBike"; my $resource = "business/mytestsite"; my $listid = "1bd8d1bc-e797-41ae-97c0-e3876406d0fe"; my $itemindex = 20; # globals my $digest; # create useragent my $ua = LWP::UserAgent->new( keep_alive => 1); $ua->credentials("$host:443", "", $user, $pass); $ua->default_header('Accept' => "application/json;odata=verbose"); $ua->timeout( 10 ); # get digest my $response = $ua->post( "https://$host/$resource/_api/contextinfo" + ); if ($response->is_success) { my $json = decode_json $response->decoded_content; $digest = $json->{d}->{GetContextWebInformation}->{FormDigestValue +}; } else { die $response->status_line; } # post data my $msg = '{"__metadata":{"type":"SP.Data.DocLibItem"},"Title":"This + is my testtitle"}'; my $req = POST "https://$host/$resource/_api/lists('$listid')/items( +$itemindex)", "Content-type" => "application/json;odata=verbose", "X-HTTP-Method" => "MERGE", "X-RequestDigest" => $digest, "IF-MATCH" => "*", "Content" => $msg; $response = $ua->request( $req ); my $json = decode_json $response->decoded_content; print Dumper( $json ); if ($response->is_success) { print "place to be :)\n"; } else { die $response->status_line; }

        Hey thanks so much for posting this conclusion, it helped me out a lot