Reputation: 89
I'm trying to do a POST to insert values in a Sharepoint 2013 list from an AIR client. The GET works and authentication is ok.
Here is my as3 code:
URLRequestDefaults.authenticate = true;
URLRequestDefaults.setLoginCredentialsForHost("srv", "usr", "pwd");
var postURL:URLRequest= new URLRequest("http://srv/site/_api/web/lists/getByTitle('list')/items");
postURL.authenticate = true;
postURL.method = URLRequestMethod.POST;
postURL.requestHeaders.push( new URLRequestHeader("ACCEPT","application/json;odata=verbose"));
postURL.requestHeaders.push( new URLRequestHeader("ContentType","application/json"));
postURL.data = JSON.stringify({ '_metadata': { 'type': 'SP.listnameListItem' }, 'Title': 'desc' });
postLoader = new URLLoader(postURL);
postLoader.dataFormat = URLLoaderDataFormat.TEXT;
postLoader.addEventListener(flash.events.Event.COMPLETE, postDone);
postLoader.addEventListener(IOErrorEvent.IO_ERROR, handleIOError );
postLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, handleHttpStatus );
postLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleSecurityError );
postLoader.load(postURL);
When I run it, it goes to the handleIOError method:
private function handleIOError ( event:IOErrorEvent ):void
{
trace ( "Load failed: IO error: " + event.text );
trace ( "data IO error: " + event.currentTarget.data.toString() );
}
I got this error: Microsoft.Data.OData.ODataContentTypeException 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=feed, application/atom+xml, application/json;odata=verbose' matches the content type 'text/xml;charset=utf-8' Any idea? Thanks
Upvotes: 0
Views: 3083
Reputation: 89
OK, I found out adding/modifying these line made it work:
postURL.requestHeaders.push( new URLRequestHeader("ContentType","application/json;odata=verbose"));
postURL.requestHeaders.push( new URLRequestHeader("X-RequestDigest",FormDigestValue ));
postURL.requestHeaders.push( new URLRequestHeader("If-Match","*"));
postURL.requestHeaders.push( new URLRequestHeader("X-Http-Method","POST"));
postURL.contentType="application/json;odata=verbose";
Upvotes: 2