+ Start a Discussion
geeta garggeeta garg 

WebService

Hi,  
This is the request part of Webservice Api.
curl -v -u user@yourcompany.com:test -F "attachments[]=@/path/to/attachment1.ext" -F "attachments[]=@/path/to/attachment2.ext" -F "email=example@example.com" -F "subject=Title" -F "description=this is a sample" -X POST 'https://domain.test.com/api'
 I want to use this request in Apex class. How can i use it? Please suggest.  
Thanks,
Geeta Garg
Krishna SambarajuKrishna Sambaraju
Can you just describe your use case instead of the sample code above? Will be able to help you, if I know what you are trying to do.

Regards,
Krishna.
geeta garggeeta garg
Hi,

I am trying to upload attachment with other content into another third party using API.Above written code is the request code for this.I did not understand how to use it.

Thanks,
Geeta Garg
Krishna SambarajuKrishna Sambaraju
Try the following code.
String endPoint = '<end point url goes here>';
HttpRequest req = new HttpRequest();
req.setHeader('Content-Type','multipart/form-data');
req.setMethod('POST');
req.setEndpoint(endPoint);
req.setBodyAsBlob(<attachment body goes here>);
req.setTimeout(60000);
req.setHeader('Authorization', 'Bearer '+token);//set authorization headers if any
req.setHeader('Content-Length',String.valueof(req.getBodyAsBlob().size()));
Http http = new Http();
HTTPResponse res = http.send(req);
I think there is a limit of 3MB for sending attachments through callouts from salesforce. Here is a link for more details.
http://salesforce.stackexchange.com/questions/51162/what-is-the-max-size-of-an-attachment-the-we-can-move-to-3rd-party-server-using

Hope this helps.

Regards,
Krishna.
geeta garggeeta garg
Hi Krishna,

I am trying this with postman tool. But i am getting error 502 Bad Gateway. 
I am attaching some screenshots so that you can tell me what i am doing wrong.
User-added image
User-added image

Thanks,
Geeta Garg

 
Krishna SambarajuKrishna Sambaraju
This seem to be an issue with the end point. Check with the developers who built the API, which your are trying to connect to.
geeta garggeeta garg
Hi Krishna,

Please see these screenshot of postman and it is working in postman.But In apex code, it is not working.

User-added image
User-added imageUser-added image

Please suggest me what i will change so that it will work.

Thanks
Geeta Garg

 
Krishna SambarajuKrishna Sambaraju
Are you still getting 502 error? Did you add this end point in Remote Sites? 
geeta garggeeta garg
Hi Krishna,

I added the endpoint and i am getting Bad Request 400 error.
I think that i am given all the values in wrong format.

Thanks,
Geeta Garg