Skip to content

Instantly share code, notes, and snippets.

@DJCordhose
Last active April 9, 2018 14:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DJCordhose/4693922 to your computer and use it in GitHub Desktop.
Save DJCordhose/4693922 to your computer and use it in GitHub Desktop.
Ultradox complex POST parameters
function postComplex() {
// to show that stringifying real dates does work as well
var date = new Date();
var events = {
name: "Olli",
events: [
{
title: "Strategy Meeting",
startTime: "Fri Feb 01 10:00:00 CET 2013",
endTime: "Fri Feb 01 11:30:00 CET 2013",
description: "Discussion of the floreysoft strategy",
location: "Telemannstraße 22, 20255 Hamburg"
}, {
title: "Dinner",
startTime: "Fri Feb 01 20:00:00 CET 2013",
endTime: date,
description: "Dinner with Le Chef",
location: "Gaußstraße 3, 22765 Hamburg"
}
]
};
var options = {
contentType: "application/json",
method : "post",
payload : Utilities.jsonStringify(events)
};
var response = UrlFetchApp.fetch('http://www.ultradox.com/ultradoc/execute?id=kNjCQaAALBCCtRH7gSJvasHF5fqJIW&action=DOWNLOAD&locale=DE',options)
var blob = response.getBlob();
var code = response.getResponseCode();
Logger.log(code === 200 ? "Success" : "Error Code: " + code);
// sending out email just for demo purposes
// in real applications send out personalized emails using Ultradox instead
GmailApp.sendEmail("[your email goes here to send this to yourself]", "Your generated document", "See attachment",
{ attachments: [
{fileName: blob.getName(), mimeType: "application/pdf", content: blob.getBytes()}
]
});
}
import static com.google.appengine.api.urlfetch.FetchOptions.Builder.withDefaults;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.urlfetch.HTTPHeader;
import com.google.appengine.api.urlfetch.HTTPMethod;
import com.google.appengine.api.urlfetch.HTTPRequest;
import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@SuppressWarnings("serial")
public class App_Engine_SandboxServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
// this example requires GSON: http://code.google.com/p/google-gson/
URLFetchService urlFetchService = URLFetchServiceFactory
.getURLFetchService();
URL url = new URL(
"http://www.ultradox.com/ultradoc/execute?id=kNjCQaAALBCCtRH7gSJvasHF5fqJIW&action=DOWNLOAD&locale=DE");
HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST,
withDefaults().setDeadline(30.0));
Events events = new Events("Olli");
events.addEvent(new Event("Strategy Meeting", new Date(113, 01, 01,
10, 00), new Date(113, 01, 01, 11, 30),
"Discussion of the floreysoft strategy",
"Telemannstraße 22, 20255 Hamburg"));
events.addEvent(new Event("Dinner", new Date(113, 01, 01,
20, 00), new Date(113, 01, 01, 22, 30),
"Dinner with Le Chef",
"Gaußstraße 3, 22765 Hamburg"));
// use ISO8601 date format for best compatibility
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssX").create();
String json = gson.toJson(events);
request.setPayload(json.getBytes("UTF-8"));
request.setHeader(new HTTPHeader("Content-Type", "application/json"));
HTTPResponse response = urlFetchService.fetch(request);
int code = response.getResponseCode();
if (code == HttpURLConnection.HTTP_OK ) {
byte[] content = response.getContent();
resp.getOutputStream().write(content);
}
}
public static class Events {
String name;
List<Event> events;
public Events(String name) {
this.name = name;
events = new ArrayList<Event>();
}
public void addEvent(Event event) {
events.add(event);
}
}
public static class Event {
String title;
Date startTime;
Date endTime;
String description;
String location;
public Event(String title, Date startTime, Date endTime,
String description, String location) {
super();
this.title = title;
this.startTime = startTime;
this.endTime = endTime;
this.description = description;
this.location = location;
}
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class DownloadComplex {
public static void main(String[] args) throws Exception {
URL url = new URL(
"http://www.ultradox.com/ultradoc/execute?id=kNjCQaAALBCCtRH7gSJvasHF5fqJIW&action=DOWNLOAD&locale=DE");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoInput(true);
connection.setDoOutput(true);
// use ISO8601 date format for best compatibility
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssX").create();
Events events = new Events("Olli");
events.addEvent(new Event("Strategy Meeting", new Date(113, 01, 01,
10, 00), new Date(113, 01, 01, 11, 30),
"Discussion of the floreysoft strategy",
"Telemannstraße 22, 20255 Hamburg"));
events.addEvent(new Event("Dinner", new Date(113, 01, 01,
20, 00), new Date(113, 01, 01, 22, 30),
"Dinner with Le Chef",
"Gaußstraße 3, 22765 Hamburg"));
String json = gson.toJson(events);
byte[] bytes = json.getBytes("UTF-8"); // encoding is important when you are outside of ASCII
connection.setRequestProperty("Content-Length",
Integer.toString(bytes.length));
OutputStream os = connection.getOutputStream();
os.write(bytes);
os.close();
InputStream inputStream = connection.getInputStream();
FileOutputStream outputStream = new FileOutputStream(new File(
"processed.pdf"));
transfer(inputStream, outputStream);
}
private static void transfer(InputStream inputStream,
OutputStream outputStream) throws IOException {
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
}
public static class Events {
String name;
List<Event> events;
public Events(String name) {
this.name = name;
events = new ArrayList<Event>();
}
public void addEvent(Event event) {
events.add(event);
}
}
public static class Event {
String title;
Date startTime;
Date endTime;
String description;
String location;
public Event(String title, Date startTime, Date endTime,
String description, String location) {
super();
this.title = title;
this.startTime = startTime;
this.endTime = endTime;
this.description = description;
this.location = location;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment