Skip to Content
Technical Articles
Author's profile photo David Da Silva

Create a pdf attachment using the Attachments API in S/4HANA Cloud

Welcome to this short blog post showing you everything you need to add an attachment to an object in S/4HANA Cloud by using the Attachments API and Postman. I wanted to write this blog post as I couldn’t find an easy to follow blog post on how to use this API to create a pdf attachment for S/4HANA Cloud.

This blog post assumes that you have already set up the necessary communication arrangement(s) and that you have access to the API with a technical user. If you need help setting up communication arrangements, you can go here. If you don’t know which communication scenario you need to activate, please refer to the Attachments API link above.

Walkthrough

Open up Postman and create a new HTTP request, then fill out the request URL and Authorization (will likely be Basic Authentication for Username/Password).

 

Navigate to the Headers tab and create the following headers:

  • x-csrf-token, for those that do not know, you will first need to do a HEAD or GET request with the value “fetch” in order to retrieve a token to be used in the POST request when we attach the file.
  • slug, this will be the name of the file saved in S4HC
  • Content, this is the file location (Note: you should be able to execute the request with these headers and no Body, however if it doesn’t work for you, please provide a Body as explained later)
  • Content-Type, the file type of the file
  • BusinessObjectTypeName, this is the ID of the object type you want to attach to. My example is for a Sales Order which is value BUS2000116
  • LinkedSAPObjectKey, this is the key or number of the object you want to attach to. My example is for a Sales Order number 8000099999

You are almost ready to attach a file through the API!

Navigate to the Body tab, and select the radio button for “binary”. From here you can select the file you want to upload.

Everything is now in place to attach the file through the API. Now follow these steps

  1. set the value of the x-csrf-token header to fetch
  2. execute a HEAD or GET request to retrieve an x-csrf-token
  3. once you have this token, copy and paste it’s value over the fetch value in the x-csrf-token request header
  4. change the HTTP request to POST now and execute the request

You should see the following successful message:

If you get an error message, then please check the SAP community or comment here and I can help if I can.

If it is successful for you, then navigate to the object and try open the file to ensure it all worked as expected!

I hope you found this bog post useful.

If you have any thoughts, ideas, or feedback, then I encourage you to comment below!

Additionally, if you would like to do further reading around this topic, then please use these great resources: SAP S/4HANA Community page, SAP Q&A page, other SAP blogs.

If you would like similar content, then follow my profile where I will be publishing similar blog posts for easy-to-follow technical insights.

Assigned Tags

      11 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo David Da Silva
      David Da Silva
      Blog Post Author

      This API can attach several different types of objects but these need to be done in a different way.

      Please comment and let me know if you'd like to see blog posts on more of the Attachments API functionality.

      Author's profile photo Varun Venkat
      Varun Venkat

      Hi David,

      thanks for your blog post, it was very well written and easy to follow along! Unfortunately, the request doesn't seem to work for me and I'm getting the following error: "You have no authorization to execute action Edit for the document".

      I can't seem to understand the reason for the error, as my communication user has been setup to allow for a Sales order integration.

      Have attached 2 screenshots showing my request body + error message, as well as the setup of my communication arrangement. Would appreciate and guidance you can offer on this issue!

      Thanks and best regards
      Varun

      Request%20body%20and%20error%20message

      Author's profile photo Varun Venkat
      Varun Venkat

      In case anyone else runs into a similar error, here is the solution that worked for me.

      In the headers tab, use 'BUS2032' as the value for the key 'BusinessObjectTypeName' instead of 'BUS2000116'. This seemed to do the trick for me, hope it helps!

      Author's profile photo David Da Silva
      David Da Silva
      Blog Post Author

      Thank you Varun for sharing your experiences!

      Author's profile photo Babu Lal Limba
      Babu Lal Limba

      Hello David

      A very nice and simple blog to solve the attachment problem exactly I was looking for.

      Could you help me how to find the BusinessObjectTypeName for any business object? I am looking the type for Billing Document and Billing Document Request.

       

      Regards

      Babu

      Author's profile photo David Da Silva
      David Da Silva
      Blog Post Author

      Hi Babu,

      The way I got around this was to create a Custom Analytical Query on the CDS views that holds this data. I found it was case-sensitive so bear that in mind when searching.

      Kind regards,

      David

      Author's profile photo ZFCG CFTZ
      ZFCG CFTZ

      Hello David,

      I am calling get attachment but getting error saml2=disabled

       

      any idea why?

       

      thanks

      Author's profile photo David Da Silva
      David Da Silva
      Blog Post Author

      Hi,

      What type of authentication are you using?

      Some screenshot or error text may help us find the correct solution if you can share those too.

      Kind regards,

      David

      Author's profile photo Khalid El Kourati
      Khalid El Kourati

      Hi David,

       

      I am using the API for Attachment on S/4 On Premise as seen available here: https://api.sap.com/api/OP_API_CV_ATTACHMENT_SRV_0001/path/post_AttachmentContentSet

       

      I have followed the steps from you guide to create a PDF attachment to a Debit note.

      The file appears succesfully in the list of attachments for the document but when I try to open Adobe Acrobat reader tells me "Adobe Reader could not open because it is not either a supported file type or the file has been damaged".

       

      I have tried with a flat text file and this time, the file can be opened from S/4 in transaction VA03 but the file stored is empty.

       

      Is there some configuration missing?

       

      Regards,

       

      Khalid

      Author's profile photo Tipu Javed
      Tipu Javed

      Hi David,

      Thanks for your blog. Very simple and easy to follow.

      I have one question, how can i change the Document info record Document type in it?

      Currently it is posting GOS (which I can see in your screenshot as well). I want to use a different doc type.

       

      Regards,
      Tipu

       

      Author's profile photo Tipu Javed
      Tipu Javed

      Sorted out myself.

      I was using BusinessObjectTypeName BUS2038 for Maintenance Notification, it was creating GOS document type.

      I changed it PMQMEL and it picked up YP1 document type which I wanted.

      (S4HCloud: Public Edition)

      Best Regards,

      Tipu