acumaticaacumatica-kb

Having a problem uploading image to Stock Items via REST API


I'm trying to use the REST API to upload an image file to the Stock Items record, where the image is provided from an https URL. I'm using this PUT call (on my local dev instance):

http://localhost/Acumatica/entity/Default/23.200.001/StockItem/010005/Files/https://client.blob.core.windows.net/sitedocs/imagegallery/lineart/a-sample-image-1.jpg

I get the following error:

[HttpException (0x80004005): A potentially dangerous Request.Path value was detected from the client (:).]

How can I get around this? Can I not obtain an image from an https URL as I'm trying to do?
What would be a good alternative?

Thanks much...


Solution

  • Here is an example how to attach a file to the Stock Item record.

    var client = new RestClient("BASE_URL");//https://localhost
    
    //Login into the system
    var loginRequest = new RestRequest("/Acumatica/entity/auth/login", Method.Post);
    loginRequest.AddHeader("Content-Type", "application/json");
    var body = @"{" + "\n" +
        @"  ""name"" : ""USERNAME""," + "\n" +
        @"  ""password"": ""PASSWORD""" + "\n" +
    @"}";
    loginRequest.AddStringBody(body, DataFormat.Json);
    RestResponse response = client.Execute(loginRequest);
    
    //Read the file as a byte[]
    byte[] fileData = File.ReadAllBytes("C:\\Users\\USER\\Downloads\\test.jpg");
    //Create a PUT request using the ID of the Item and the name of the file.
    var attachFileRequest = new RestRequest("/Acumatica/entity/Default/23.200.001/files/PX.Objects.IN.InventoryItemMaint/Item/2ea2f35a-072f-ed11-835b-1293d93768b3/test.jpg", Method.Put);
    attachFileRequest.AddHeader("Content-Type", "application/octet-stream");
    attachFileRequest.AddBody(fileData, "application/octet-stream");
    var response2 = client.Execute(attachFileRequest);
    
    //Logout from the system
    var logoutRequest = new RestRequest("/Acumatica/entity/auth/logout", Method.Post);
    logoutRequest.AddHeader("Content-Type", "application/json");
    logoutRequest.AddStringBody(body, DataFormat.Json);
    RestResponse response3 = client.Execute(logoutRequest);