<rss version="2.0" xmlns:a10="http://www.w3.org/2005/Atom"><channel><title>Inflectra Customer Forums: Uploading and associating attachments using REST API (Thread)</title><description> If you want to upload an attachment using REST API and optionally associate it to an artifact in a project in Spiraplan, use the following code:   Python Code:    import requests
import json
import base64
from pathlib import Path

# Change the path to the your file path. Filename will be read from path.
file = Path(r'C:\Users\Documents\Files\Capture.PNG')
filename = file.name

# ArtifactTypeId (Requirement=1, Test Cases=2, Incident=3, Test Run=5, Test Sets=8)
# ArtifactId is the spiraplan id for the selected artifact type
ArtifactId = 25991
ArtifactTypeId = 3

# Endpoint URL. 44 is the project id.
URL = "https://api.inflectra.com/SpiraPlan/Services/v6_0/RestService.svc/projects/44/documents/file?username=username&amp;api-key={API-KEY}"

# Headers for the endpoint
headers = {"Accept": "application/json", "Content-Type" : "application/json"}

# Encoding the file to base64 format
with open(file, 'rb') as fd:
    b64data = base64.b64encode(fd.read()).decode("utf-8")

# Generating payload to just upload the attachment to a project
payload = f'{{"AttachmentTypeId": 1, "BinaryData": "{b64data}", "FilenameOrUrl": "{filename}"}}'

# Generating payload to upload and associate the attachment to an artifact in a project
payload = f'{{"AttachmentTypeId": 1, "BinaryData": "{b64data}", "FilenameOrUrl": "{filename}", "AttachedArtifacts": [{{"ArtifactId": {ArtifactId}, "ArtifactTypeId": {ArtifactTypeId}}}]}}'

# Calling POST method 
response = requests.post(URL, headers=headers, data = payload)
print(response)
print(response.text)
       Postman:   URL:  https://api.inflectra.com/SpiraPlan/Services/v6_0/RestService.svc/projects/44/documents/file?username=username&amp;api-key={API-KEY}   Header:  Accept: application/json, Content-Type: application/json  Body (Type: Raw-JSON) :  {AttachmentTypeId: 1, BinaryData: VGVzdGluZyBmaWxlIHVwbG9hZA==, FilenameOrUrl: Test_Postman.txt, AttachedArtifacts: [{ArtifactId: 25991, ArtifactTypeId: 3}]}  Optionally you can  generate the code  for the post method from within Postman by selecting the language of your choice. </description><language>en-US</language><copyright>(C) Copyright 2006-2026 Inflectra Corporation.</copyright><managingEditor>support@inflectra.com</managingEditor><category domain="http://www.dmoz.org">/Computers/Software/Project_Management/</category><category domain="http://www.dmoz.org">/Computers/Software/Quality_Assurance/</category><generator>KronoDesk</generator><a10:contributor><a10:email>support@inflectra.com</a10:email></a10:contributor><a10:id>http://www.inflectra.com/kronodesk/forums/threads</a10:id><ttl>120</ttl><link>/Support/Forum/spiraplan/issues-questions/2176.aspx</link><item><guid isPermaLink="false">threadId=2176</guid><author>Niraj Chaurasia (niraj.chaurasia@alticeusa.com)</author><category domain="http://www.inflectra.com/kronodesk/thread/tag"> api</category><category domain="http://www.inflectra.com/kronodesk/thread/tag">rest api</category><category domain="http://www.inflectra.com/kronodesk/thread/tag">upload attachment</category><title>Uploading and associating attachments using REST API</title><description> If you want to upload an attachment using REST API and optionally associate it to an artifact in a project in Spiraplan, use the following code:   Python Code:    import requests
import json
import base64
from pathlib import Path

# Change the path to the your file path. Filename will be read from path.
file = Path(r'C:\Users\Documents\Files\Capture.PNG')
filename = file.name

# ArtifactTypeId (Requirement=1, Test Cases=2, Incident=3, Test Run=5, Test Sets=8)
# ArtifactId is the spiraplan id for the selected artifact type
ArtifactId = 25991
ArtifactTypeId = 3

# Endpoint URL. 44 is the project id.
URL = "https://api.inflectra.com/SpiraPlan/Services/v6_0/RestService.svc/projects/44/documents/file?username=username&amp;api-key={API-KEY}"

# Headers for the endpoint
headers = {"Accept": "application/json", "Content-Type" : "application/json"}

# Encoding the file to base64 format
with open(file, 'rb') as fd:
    b64data = base64.b64encode(fd.read()).decode("utf-8")

# Generating payload to just upload the attachment to a project
payload = f'{{"AttachmentTypeId": 1, "BinaryData": "{b64data}", "FilenameOrUrl": "{filename}"}}'

# Generating payload to upload and associate the attachment to an artifact in a project
payload = f'{{"AttachmentTypeId": 1, "BinaryData": "{b64data}", "FilenameOrUrl": "{filename}", "AttachedArtifacts": [{{"ArtifactId": {ArtifactId}, "ArtifactTypeId": {ArtifactTypeId}}}]}}'

# Calling POST method 
response = requests.post(URL, headers=headers, data = payload)
print(response)
print(response.text)
       Postman:   URL:  https://api.inflectra.com/SpiraPlan/Services/v6_0/RestService.svc/projects/44/documents/file?username=username&amp;api-key={API-KEY}   Header:  Accept: application/json, Content-Type: application/json  Body (Type: Raw-JSON) :  {AttachmentTypeId: 1, BinaryData: VGVzdGluZyBmaWxlIHVwbG9hZA==, FilenameOrUrl: Test_Postman.txt, AttachedArtifacts: [{ArtifactId: 25991, ArtifactTypeId: 3}]}  Optionally you can  generate the code  for the post method from within Postman by selecting the language of your choice. </description><pubDate>Tue, 26 May 2020 17:11:24 -0400</pubDate><a10:updated>2022-01-19T20:58:43-05:00</a10:updated><link>/Support/Forum/spiraplan/issues-questions/2176.aspx</link></item><item><guid isPermaLink="false">messageId=3781</guid><author>Niraj Chaurasia (niraj.chaurasia@alticeusa.com)</author><title> API Documentation URL:  https://api.inflectra.com/Spira/Services/v6_0/RestServiceOperation.aspx?uri</title><description> API Documentation URL:  https://api.inflectra.com/Spira/Services/v6_0/RestServiceOperation.aspx?uri=projects%2f%7bproject_id%7d%2fdocuments%2ffile&amp;method=POST  </description><pubDate>Tue, 26 May 2020 17:25:25 -0400</pubDate><a10:updated>2020-05-26T17:25:25-04:00</a10:updated><link>/Support/Forum/spiraplan/issues-questions/2176.aspx#reply3781</link></item><item><guid isPermaLink="false">messageId=3783</guid><author>David J (adam.sandman+support@inflectra.com)</author><title> Thanks :-) </title><description> Thanks :-) </description><pubDate>Tue, 26 May 2020 17:56:48 -0400</pubDate><a10:updated>2020-05-26T17:56:48-04:00</a10:updated><link>/Support/Forum/spiraplan/issues-questions/2176.aspx#reply3783</link></item></channel></rss>