Attachment body returns when using curl but is empty when using Postman or Salesforce

Does anyone know why I can retrieve an attachments body using curl but using either postman or Salesforce it returns an empty body? The requests using postman or salesforce both return 200 OK and give no signs of errors in the header, yet the body is still empty.

1 Like

You mind posting your curl command here? (include everything but obfuscate anything sensitive/confidential)

Sure, here you go.

curl -L -f -D- -u username:password -X GET -H “Content-Type: application/json” “https://instancename.atlassian.net/secure/attachment/13733/” -o “testfile.txt”

@collin.parker - try using:

https://instancename.atlassian.net/rest/api/2/attachment/13733

See https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-attachment-id-get

1 Like

So that gives the metadata about the attachment, what I am trying to get is the actual attachment data. I am using the content url from this metadata and it only works in curl.

When I call the content URL using both curl and Postman, I’m able to get the binary data in the body using the /secure/attachment/123/ method type. Something fishy we’re missing here. In your Postman settings, do you have Automatically follow redirects turned on? (that endpoint sends a 302)

1 Like

I was using the deprecated browser version of postman, using the standalone it works fine. The problem seems to be that Salesforce does not automatically follow redirects. But shouldn’t JIRA respond with a 302 in that case so I can make another request to the redirected site manually?

I still can’t get attachment retrieval working with Salesforce, is there a workaround available for clients that do not support automatically redirecting?

Can you provide more specifics about your integration work between Jira and Salesforce? For example, where your Salesforce app is running, and which dev environment you’re using.

The app is running on salesforce, written as Apex. I can send you over the test code if that would be helpful? Thanks for all the help so far!

@collin.parker Sure, that would be swell. If you’re more comfortable sending it to me in private, go ahead and send me an email (nmansilla@atlassian.com) or a private message here in the Dev forums.

Hi parker,

I was able to resolve this issue.

Issue:
As expected , HTTP Callouts from salesforce doesn’t handle URL redirection

Resolution :

An HTTP response with this status code will additionally provide a URL in the location header field. Which can be used for making further callout to get the body, so when you get this status code you need to make a second request using the value in the location header of the first response.

HttpResponse res = new Http().send(req);
while (res.getStatusCode() == 302) {
req.setEndpoint(res.getHeader(‘Location’));
res = new Http().send(req);
}

URL(which helped me) : https://salesforce.stackexchange.com/questions/136811/httpcallout-error-statuscode-302

Hope this would help you, if you haven’t resolved this.

Regards,
Chandra.

2 Likes

I did eventually get this resolved just as you have stated, my apologizes for not updating the post.