2

I fetch data from Sharepoint using perl on a linuxmachine. This worked real good as long as I only had to read data from there.

Now that I also have to change data on Sharepoint I'm stuck.

Sharepoint throws:

A supported MIME type could not be found that matches the content type of the response. None of the supported type(s) 'application/atom+xml;type=entry, application/atom+xml, application/json;odata=verbose' matches the content type 'application/json'.

It's almost the same problem as this one, except that my Content-type seems to be correct and the error still gets thrown:

ODataContentTypeException : Content Type 'application/json;odata=verbose'

After wasting two days with trying and googling I now hope that someone here can help me.

This is my code:

#!/usr/bin/perl

use strict;

use LWP::UserAgent;
use LWP::Authen::Ntlm;
use JSON;
use Data::Dumper;

# config
  my $host = "mysite.local";
  my $user = 'domain\spadmin';
  my $pass = "sucKmYducK";
  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"}';

  $ua->default_header
  (
    'Accept'          => "application/json;odata=verbose",
    'Content-type'    => "application/json;odata=verbose",
    'Content-length'  => length( $msg ),
    'X-HTTP-Method'   => "MERGE",
    'X-RequestDigest' => $digest,
    'IF-MATCH' => "*"
  );

  my $response = $ua->post
  (
    "https://$host/$resource/_api/lists('$listid')/items($itemindex)",
    {
      data => $msg
    }
  );

  my $json = decode_json $response->decoded_content;
  print Dumper( $json );

  if ($response->is_success)
  {
    print "place to be :)\n";
  }
  else
  {
    die $response->status_line;
  }
0

After some more digging I could solve that problem.

The default_header-directive doesn't send the content-type-header. It is necessary to use the HTTP::Request-module to make it work.

Now Sharepoint accepts the post which brings me one step further, but not to the end since I now get a new error: "Invalid JSON. A token was not recognized in the JSON content.".

I'll post a new question for that...

#!/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 = "sucKmYducK";
  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"}';
  $msg = "";

  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"  => { data => $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;
  }

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.