cancel
Showing results for 
Search instead for 
Did you mean: 

B1 Service Layer: create attachment from POST

0 Kudos

Hi everyone,

I'm using the B1 Service Layer to create and retrieve attachments from a company database. The attachments folder has been set correctly in the settings of Business One, and is set to a Windows share. This has been in use for a while now.

A business need has come up to create and retrieve attachments with HANA XS through the B1 Service Layer.

I followed the instructions in the "Working with SAP Business One Service Layer" manual, and succesfully mounted the attachments folder to the HANA Linux machine. When I create a file using the Linux bash in the /mnt/Attachments folder, I can see it immediately appearing in the Windows share that contains the attachments.

When I retrieve an attachment using the service layer (GET https://xxx:50000/b1s/v1/Attachments2(11452)/$value?filename='test.xls') this works as well. I get the binary representation of the file back in the body of the response, and in the headers I can find the mime-type of the file.

However, when I want to create a new attachment through a POST call, I keep getting the following back:

{ "error": { "code": -1000, "message": { "lang": "en-us", "value": "Fail to create attachment folder" } } }

I execute a POST call to https://xxx:50000/b1s/v1/Attachments2. The request contains a Content-Type header which is set to multipart/form-data;boundary=Attachment.

The body of the request looks like this:

--Attachment
Content-Disposition: form-data; name="files"; filename="test_20171123124300.txt"
Content-Type: text/plain


This is a test file
--Attachment--

The user of the Service Layer (b1service0) is the owner of the /mnt/Attachments folder. When I use this user for creating a new file in this directory (which mounts to the Windows share), this works as well.

It tried multiple file types, but nothing works.

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

Hi Ankit,

I was able to resolve it myself. The issue had to do with insufficient priviliges for the /tmp folder on the HANA server.

The Service Layer runs under the b1service0 user. The /tmp folder has 0755 permissions.

When the Service Layer tries to create attachments, it will first create the attachments in the /tmp folder, and will then move the attachments to the mounted attachment folder. For this, it creates a folder structure in the /tmp folder: /tmp/sap_b1_b1service0/ServiceLayer/Attachments2

The /tmp folder needs 0777 permissions for the b1service0 user to be able to create this folder structure on the first run.

After doing a sudo chmod -R 0777 /tmp it worked as expected.

ANKIT_CHAUHAN
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Bert,

I thought you might have done it already (as a setup) and I just send you the JSON format to create the attachment.

Anyways, you can refer to SAP Note 2312947 for the same.

Kind regards,

ANKIT CHAUHAN

SAP SME Support

Answers (2)

Answers (2)

0 Kudos

Hi Ankit,

Thank you for your answer.

The suggested call leads to the following response:

{
    "odata.metadata": "https://xxx:50000/b1s/v1/$metadata#Attachments2/@Element",
    "AbsoluteEntry": "11498",
    "Attachments2_Lines": [
        {
            "SourcePath": "C:\tmp",
            "FileName": "test_file",
            "FileExtension": "txt",
            "AttachmentDate": "2017-11-23",
            "UserID": "1",
            "Override": "tNO",
            "U_CorCategory": null
        }
    ]
}

So the attachment entry itself is created in the SAP database (hence the new AbsoluteEntry), but the file itself is not physically created in the attachments folder. That is why I'm trying to achieve with the call above, and which is documented in this blog post (which made it into the official documentation for the Service Layer.

ANKIT_CHAUHAN
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi,

Check whether it works?

POST Request: https://xxx:50000/b1s/v1/Attachments2
{ 
"AbsoluteEntry" : "1", 
"Attachments2_Lines" : [ 
{ 
"SourcePath" : "C:\tmp", 
"FileName" : "test_file", 
"FileExtension" : "txt" 
} 
] 
} 

Also can change your Content-Type Header to "multipart/mixed" and test the issue with the sample code I provided above?

Kind regards,

ANKIT CHAUHAN

SAP SME Support