This knowledge article may contain information that does not apply to version 21.05 or later which runs in a container environment. Please refer to Article Number 000385088 for more information about troubleshooting BMC products in containers. How to send attachments using TestHTTPClient Follow the article https://communities.bmc.com/docs/DOC-129733 for further details. The main structure follows. Please adhere to HTTP protocol where newline characters and blank lines are important @boundary= 854185817597954740109521 @schema = http @host= localhost:8008 @token=AR-JWT 1245 POST {{schema}}://{{host}}/api/arsys/v1/entry/zzTestAttachment Authorization: {{token}} Content-Type: multipart/form-data; boundary={{boundary}} --{{boundary}} Content-Transfer-Encoding: 8bit Content-Type: application/json; charset=UTF-8 Content-Disposition: form-data; name="entry" {"values":{"Submitter":"Demo", "Short Description":"testing 123", "Attachment":"file.png"}} --{{boundary}} Content-Disposition: form-data; name="attach-Attachment"; filename="file.png" Content-Type: application/octet-stream Content-Transfer-Encoding: binary <<< C:\tmp\rainbow.png --{{boundary}}-- USING POSTMAN Important: POSTMAN must be Version 7.6.0 or newer to perform this. To send attachments using Postman either creating an entry (POST) or updating an existing one (PUT) you must follow these instructions. The incident creates an interface for this example will be used. 1. Set your URL and method, in this case, an attachment is going to be uploaded to an existing incident 2. Set your authentication header as in any other call, you can use Postman variables like in this case {{token}} to replace the full token provided by AR Server 3. Create a file with the JSON content that you want to send over the body, in this case, a file named data_entry.txt was created with the following content.
{
"values": {
"z1D_Details": "Affected Hosts Updated by Tenable Automation. See attachment",
"z1D_WorklogDetails": "The IPs affected have changed",
"z1D Action": "MODIFY",
"z1D_View_Access": "Internal",
"z1D_Secure_Log": "Yes",
"z1D_Activity_Type": "Incident Task/Action",
"Detailed Decription": "Updated description",
"z2AF_Act_Attachment_1":"sample.xml"
}
}
Make sure the attachment field (in this case z2AF_Act_Attachment_1) has a value with the name of the file you want to attach 4. In POSTMAN, change the body to use the Form-Data option, this is needed because the request will consist of 2 parts, JSON, which contains the request, and Octet-stream which will contain the binary content of the file. Click the 3 dots at the end of the table below and make sure Content-Type is checked. 5. You will need two keys (one for the JSON content and one for the attachment) for both keys you must change the type from text to file 6. Open the file where you save your JSON content for the first key (data_entry.txt in this example) and set the content type as application/json the key name can be whatever you want. for the second key select the attachment you want to upload, set the content type as application/octet-stream and the structure of the name of the key should be attach-fieldname in this case attach-z2AF_Act_Attachment_1, the field it's very important always use attach- and then the exact name of the attachment field otherwise your attachment won't upload. This is how the section will look like. 7. Submit the request. To do the same operation but with a POST (to create the incident with the attachment in it) below is an example of the JSON payload, you only need to change the method to POST and use the payload below, the rest of the instructions would be the same as above
{
"values":{
"z1D_Action":"CREATE",
"Last_Name":"Allbrook",
"First_Name":"Allen",
"Description":"Test REST API Incident test",
"Impact": "1-Extensive/Widespread",
"Urgency": "1-Critical",
"Reported Source": "Direct Input",
"Submitter": "Allen",
"Service_Type": "User Service Restoration",
"Company": "Calbro Services",
"z1D_Details": "Incident Created using REST API",
"z1D_WorklogDetails": "Attachment using REST API",
"z1D_View_Access": "Internal",
"z1D_Secure_Log": "Yes",
"z1D_Activity_Type": "Incident Task/Action",
"z2AF_Act_Attachment_1":"sample.xml"
}
}
JAVA EXAMPLE If you are looking for a Java example, use the attached file - RESTAttachmentExample.java. This example assumes the use of a form called frmTest which has an attachment pool with one attachment field. The other RESTAttachmentWorkInfo.zip gives an example of creating an entry with an attachment to the WOI:WorkInfo form of an existing work order. . The zip file contains the .java file and a readme file that provides guidance on what needs to be updated in the .java file along with instructions on how to execute it. Finally, we have included another JAVA code example in the RESTAttachmentWorkInfoIncident.ZIP folder. This example creates an entry with an attachment to the HPD:WorkLog form of an existing incident. The zip file contains: the .java file, the attached TXT file, and a Readme file (in this file provides the information on what to update in the .java file, along with instructions on how to run it) CURL EXAMPLE (Note Token was truncated for explanation purposes) We created a sample form ZtestRestAttach {"values":{"Short Description__c":"REST TEST","Submitter__c":"REST TEST","Status__c":"New","Attachment__c":"file.txt"}} and file.txt (any content) There are two parts that should match any attachment: in the entry_data make sure that Attachment__c matches the attachment Name "attach_Attachment__c" In that way, each attachment would be saved with the proper filename and in the proper attachment location. As there are two filenames (the actual filename and the filename specified in the JSON data) the one that would be saved is the one on the JSON structure. Windows PowerShell EXAMPLE We created a sample form: HPD:IncidentInterface_Create This example you need to declare the location where the file is located as: $FilePath = 'C:\Pepe1.pdf' Also, in the body information, it is necessary to add the file name and its extension to save it in the form. Example: "{`"values`":{`"z2AF_Act_Attachment_1`":`"Pepe1.pdf`", It should be noted that file encoding may fail if PowerShell detects any special characters or if the attached file is corrupted. In addition, the encoding that is being used may not work for other types of files Example: $response=Invoke-RestMethod 'http://ARservername/api/jwt/login' -Method 'POST' -ContentType 'application/x-www-form-urlencoded' -Body 'username=yourusername&password=yourpassword' $response | ConvertTo-Json $token="AR-JWT "+$response $RemedyServer = 'ARservername' $Headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Content-Type", "application/json") $headers.Add("Authorization",$token) $FilePath = 'YourFilepathwiththefilenamewithextention' $uri = "http://" + $RemedyServer + ":8008/api/arsys/v1/entry/HPD:IncidentInterface_Create" $fileBytes = [System.IO.File]::ReadAllBytes($FilePath); $fileEnc = [System.Text.Encoding]::GetEncoding('iso-8859-1').GetString($fileBytes); $boundary = [System.Guid]::NewGuid().ToString(); $LF = "`n" $bodyLines = ( "--$boundary", "Content-Disposition: form-data; name=`"entry`"", "Content-Type: application/json; charset=UTF-8", "Content-Transfer-Encoding: 8bit$LF", "{`"values`":{`"z2AF_Act_Attachment_1`":`"filenamewithextention`", `n`"First_Name`":`"Allen`", `n`"Last_Name`":`"Allbrook`", `n`"Assigned Group`": `"Backoffice Support`", `n`"Description`":`"Test Change Create from REST API`", `n`"Impact`": `"1-Extensive/Widespread`", `n`"Urgency`": `"1-Critical`", `n`"Status`": `"New`", `n`"Reported Source`": `"Direct Input`", `n`"Service_Type`": `"User Service Restoration`", `n`"Assigned Support Company`" : `"Calbro Services`", `n`"Assigned Support Organization`": `"IT Support`", `n`"Assigned Group`": `"Backoffice Support`", `n`"Categorization Tier 1`": `"Information Systems`", `n`"Categorization Tier 2`": `"Cybersecurity`", `n`"Categorization Tier 3`": `"Misconfiguration`", `n`"z1D_Details`":`"Internal`" } }", "--$boundary", "Content-Disposition: form-data; name=`"attach-z2AF_Act_Attachment_1`"", "Content-Type: application/octet-stream", "Content-Transfer-Encoding: binary$LF", $fileEnc, "--$boundary--$LF" ) -join $LF $params = @{ Uri = $uri Body = $bodyLines Method = 'Post' Headers = $Headers ContentType = "multipart/form-data; boundary=$boundary" } Invoke-RestMethod @params NodeJS EXAMPLE The attachment, TextAndBinaryWorkInfo_And_fiddler.zip contains two NodeJS examples: attachmentBinaryWorkInfo.js and attachmentTextWorkinfo.js Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. In order to use the attached js examples, Node.js needs to be installed and configured in the test environment. Downloads and documentation can be found here: https://nodejs.org The node JS example will handle 2 types of attachments, text, and images. Text attachments sent in binary format look just like the text on the file File screenshot When these characters are inspected in the http traffic, they look like this: HTTP request traffic: Note that base64 encoding is not required while transferring this file. The same thing would happen for other types of files. A PNG would look like this when transferred: Extra headers like Content-Disposition, Content-Type, and Content-Transfer-Encoding are required. Fiddler capture/compose Fiddler is able to replay and edit HTTP requests with certain limitations. It cannot change the binary file contents, which should be done by a program like java or node. However, if the interest is in sending text files for testing, Fiddler should meet the need The attachment, Text AndBinaryWorkInfo_And_fiddler.zip contains a Fiddler capture called attachments_to_WOIWorkOrder.saz. This Fiddler capture contains two requests. The first one marked in red is in binary; it is there for inspection purposes only and modifying it in Fiddler is discouraged. The second request includes a simple text file and it can be edited. Here are instructions on how to edit and execute the request in Fiddler. Note: it is assumed that the Fiddler tool is installed. If not, it can be downloaded from https://www.telerik.com/download/fiddler. 1. Open the attachments_to_WOIWorkOrder.saz in Fiddler.
2. Drag the second request to the "Composer" tab 3. Change the following as it relates to your environment/request: URL, Host, AR-JWT token The syntax of the request body is very sensitive. Be very cautious when modifying. The text in the marked line (pictured below) may be changed to see how various characters are transferred. 4. Click the Execute button to submit the request Here is the raw text of the request:against a form called frmTest POST http://clm-pun-030535.bmc.com:8008/api/arsys/v1/entry/frmTest HTTP/1.1 Authorization: AR-JWT … http://https://community.bmc.com/s/news/aA33n000000CixyCAC/how-to-send-attachment-by-api-rest-with-postman-in-remedy |