Upload attachments 405 Method Not Allowed

I'm trying to upload attachments via OData.

When I run my code it everything is fine until the last save which returns 405 code Method Not Allowed.

My attachment is added and it shows on a contact that I wanted to attach to with right format and name but when

I click on attachment do download it the file is  empty I get 0kb downloaded It is like my file is not uploaded.

 

My code ==>

 

  

class Program

    {

        private const string baseUri = "http://localhost:8011";

        private static Uri serverUri = new Uri(baseUri + @"/0/ServiceModel/EntityDataService.svc/");

        private const string authServiceUtri = "http://localhost:8011/ServiceModel/AuthService.svc/Login";

        static void Main(string[] args)

        {

            CreateActivityWithAttachment();

        }

        static CookieCollection TryLogin()

        {

            var authData = @"{

            ""UserName"":""Supervisor"",

            ""UserPassword"": ""Supervisor""

            }";

            var request = CreateRequest(authServiceUtri, authData);

            var AuthCookie = new CookieContainer();

            request.CookieContainer = AuthCookie;

    

            using (var response = (HttpWebResponse)request.GetResponse())

            {

                return response.Cookies;

            }

        }

        static HttpWebRequest CreateRequest(string url, string requestData = null)

        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.ContentType = "application/json";

            request.Method = "POST";

            request.KeepAlive = true;

            if (!string.IsNullOrEmpty(requestData))

            {

                using (var requestStream = request.GetRequestStream())

                {

                    using (var writer = new StreamWriter(requestStream))

                    {

                        writer.Write(requestData);

                    }

                }

            }

            return request;

        }

        static void OnSendingRequestCookie(object sender, SendingRequestEventArgs e)

        {

            var TokenValue = TryLogin();

            var req = e.Request as HttpWebRequest;

            req.CookieContainer = new CookieContainer();

            req.CookieContainer.Add(TokenValue);

            req.Headers.Add("ForceUseSession", "true");

            req.Headers.Add("BPMCSRF", TokenValue["BPMCSRF"].Value);

            e.Request = req;

        }



        public static void CreateActivityWithAttachment()

        {

            Guid activityId = Guid.NewGuid();

            var context = new BPMonline(serverUri);

            DataServiceRequestArgs _args = new DataServiceRequestArgs();

            _args.ContentType = "application/octet-stream";

            //Read file from file system

            //byte[] bytes = System.IO.File.ReadAllBytes("C:/Users/mmaricic/DevelopmEN.pdf");

            

            using (var fs = new FileStream("C:/Users/mmaricic/DevelopmEN.pdf", FileMode.Open))

            {

                var SupervisorID = new Guid("410006E1-CA4E-4502-A9EC-E54D922D2C00");

                //Define method for authentication

                context.SendingRequest += new EventHandler(OnSendingRequestCookie);

                var allContacts = from contacts in context.ContactCollection

                                  where contacts.Id == SupervisorID

                                  select contacts;

                var CurrContacr = allContacts.FirstOrDefault();

                ContactFile NewFile = new ContactFile()

                {

                    Id = Guid.NewGuid(),

                    Name = "DevelopmEN1.pdf",

                    Size = (int)fs.Length,

                    TypeId = new Guid("529BC2F8-0EE0-DF11-971B-001D60E938C6"),

                    ContactId = SupervisorID,

                    Contact = CurrContacr,

                    SysFileStorageId = Guid.NewGuid(),

                    CreatedById = SupervisorID,

                    LockedById = SupervisorID,

                    ModifiedById = SupervisorID

                };



                context.AddToContactFileCollection(NewFile);

                context.SetLink(NewFile, "Contact", CurrContacr);

                DataServiceResponse responces = context.SaveChanges();

                context.SetSaveStream(NewFile, "Data", fs, true, "application/octet-stream");

                context.UpdateObject(CurrContacr);

                context.SaveChanges();//********* ERROR 405 ON THIS LINE *********//

            }

        }

    }

 

Like 0

Like

1 comments

Hi Marco, 

 

It's definitely should have been debugged, but on the first sight it looks like Wrong IIS settings.

 

Please have a look at the next post, it might help you to solve it:

 

https://community.creatio.com/articles/mobile-error-http-4050-method-no…

 

Also please make sure you didn't make any mistakes within making these requests, please check out this link:

 

https://kinsta.com/blog/405-method-not-allowed-error/

 

Also the general post about OData:

 

https://academy.creatio.com/docs/developer/integrations_and_api/data_se…

 

Hopefully it will help you!

 

Best Regards, 

 

Bogdan L.

 

Show all comments