Felipe Soler
Felipe Soler

Reputation: 1

BadFormat SAP B1 API Service Layer

I try to post a new attachment in SAP B1 trought SAP B1 Service Layer in the endpoint /Attachment2, this request made it by python but i Get this message:

Code Status: 400

Error uploading file: {
   "error" : {
      "code" : -1,
      "message" : {
         "lang" : "en-us",
         "value" : "BadFormat"
      }
   }
}

My python code first download the file in a tmp\files directory, here's the code

ospath = os.path.dirname(os.path.abspath(__file__))
route = ospath+"\\tmp\\files\\01. CEDULA.pdf"
data = open(route, 'rb').read()
# Set the boundary for the multipart/form-data
boundary = "--WebKitFormBoundaryUmZoXOtOBNCTLyxT"

# Set the headers
headers = {
    "Content-Type": "multipart/form-data",
    "boundary" : boundary,
    "Cookie" : response.headers['Set-Cookie']
}

with open(route, 'rb') as file:
    file_data = file

    # Set the body of the request
    body = f"""{boundary}
    Content-Disposition: form-data; name="files"; filename="01. CEDULA.pdf"
    Content-Type: application/pdf

    {file_data}

    {boundary}--"""

    response = requests.post(url+"Attachments2", headers=headers, data=body, verify=False)
    # Send the request
    print(response.status_code)
    # Check the response status code
    if response.status_code == 201:
        print("File uploaded successfully.")
    else:
        print("Error uploading file:", response.text)

My code is based on this SAP Documentation: https://sap.highwaytwo.com/Working_with_SAP_Business_One_Service_Layer.pdf

Thanks

Upvotes: 0

Views: 170

Answers (0)

Related Questions