Skip to Content
0

B1 Service Layer: create attachment from POST

Nov 23, 2017 at 12:37 PM

177

avatar image

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.

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
Bert Fonteyn Nov 27, 2017 at 09:55 AM
0

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.

Show 1 Share
10 |10000 characters needed characters left characters exceeded

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

0
ANKIT CHAUHAN
Nov 23, 2017 at 03:25 PM
0

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

Share
10 |10000 characters needed characters left characters exceeded
Bert Fonteyn Nov 23, 2017 at 03:50 PM
0

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.

Share
10 |10000 characters needed characters left characters exceeded