Skip to content

Instantly share code, notes, and snippets.

@manute
Last active December 18, 2015 12:09
Show Gist options
  • Save manute/5780480 to your computer and use it in GitHub Desktop.
Save manute/5780480 to your computer and use it in GitHub Desktop.
Multipart-post document to Scribd
@Grab(group='org.apache.httpcomponents',module='httpclient',version='4.2.5')
@Grab(group='org.apache.httpcomponents',module='httpmime',version='4.2.5')
import org.apache.http.client.HttpClient
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.mime.content.FileBody
import org.apache.http.entity.mime.MultipartEntity
import org.apache.http.HttpResponse
import org.apache.http.HttpEntity
import org.apache.http.util.EntityUtils
final API_URL = "http://api.scribd.com/api"
final API_KEY = "api_key"
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("$API_URL?method=docs.upload&api_key=$API_KEY");
//Create the file and add to multipart
FileBody bin = new FileBody(new File("/Users/Documents/test.pdf"));
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("file", bin);
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
//Parse response xml
String xmlString = EntityUtils.toString(resEntity);
def root = new XmlSlurper().parseText(xmlString)
println root.doc_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment