Skip to content

Instantly share code, notes, and snippets.

@int128
Last active August 29, 2015 14:13
Show Gist options
  • Save int128/3436d5d1dfe86a6f2400 to your computer and use it in GitHub Desktop.
Save int128/3436d5d1dfe86a6f2400 to your computer and use it in GitHub Desktop.
Making a HTTP request on Google App Engine

Making a HTTP request on App Engine

Use HttpURLClient in Groovy.

import groovyx.net.http.HttpURLClient

try {
  def client = new HttpURLClient(url: 'https://...')
  def response = client.request(query: query)
  response.data
} catch (HttpResponseException e) {
  log.severe(e.localizedMessage)
}

Tried but failed

RESTClient (Groovy)

import groovyx.net.http.HttpResponseDecorator
import groovyx.net.http.RESTClient

def response = new RESTClient('https://...').post(query: query) as HttpResponseDecorator
response.data
com.google.apphosting.api.ApiProxy$FeatureNotEnabledException: The Socket API will be enabled for this application once billing has been enabled in the admin console.
	at java.lang.Thread.getStackTrace(Thread.java:1568)
	at com.google.apphosting.runtime.ApiProxyImpl.doSyncCall(ApiProxyImpl.java:258)
	at com.google.apphosting.runtime.ApiProxyImpl.access$000(ApiProxyImpl.java:69)
	at com.google.apphosting.runtime.ApiProxyImpl$1.run(ApiProxyImpl.java:201)
	at com.google.apphosting.runtime.ApiProxyImpl$1.run(ApiProxyImpl.java:198)
	at java.security.AccessController.doPrivileged(Native Method)
	at com.google.apphosting.runtime.ApiProxyImpl.makeSyncCall(ApiProxyImpl.java:198)
	at com.google.apphosting.runtime.ApiProxyImpl.makeSyncCall(ApiProxyImpl.java:69)
	at com.google.apphosting.api.ApiProxy.makeSyncCall(ApiProxy.java:116)
	at com.google.apphosting.api.ApiProxy.makeSyncCall(ApiProxy.java:65)
	at com.google.appengine.api.socket.SocketApiHelper.apiProxyMakeSyncCall(SocketApiHelper.java:90)
	at com.google.appengine.api.socket.SocketApiHelper.makeSyncCall(SocketApiHelper.java:58)
	at com.google.appengine.api.socket.NameServiceImpl.lookupAllHostAddr(NameServiceImpl.java:61)
	at com.google.apphosting.util.ResolverManager$AppEngineNameservice.lookupAllHostAddr(ResolverManager.java:42)
	at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1388)
	at java.net.InetAddress.getAllByName0(InetAddress.java:1341)
	at java.net.InetAddress.getAllByName(InetAddress.java:1255)
	at java.net.InetAddress.getAllByName(InetAddress.java:1186)
	at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
	at org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:278)
	at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:162)
	at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
	at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:640)
	at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479)
	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1066)
	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1044)
	at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:515)
	at groovyx.net.http.RESTClient.post(RESTClient.java:141)

Dispatcher (Scala)

import dispatch._
import dispatch.Defaults._

val oauth = url("https://...").POST
oauth << Map("key" -> "value")

val http = Http(oauth OK as.String)
http.completeOption match {
  case Some(response) =>
    PlainTextContent ~> ResponseString(response)
  case None =>
    NotFound
}
java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "accessClassInPackage.sun.misc")
	at java.security.AccessControlContext.checkPermission(AccessControlContext.java:372)
	at java.security.AccessController.checkPermission(AccessController.java:559)
	at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
	at com.google.appengine.tools.development.DevAppServerFactory$CustomSecurityManager.checkPermission(DevAppServerFactory.java:429)
	at java.lang.SecurityManager.checkPackageAccess(SecurityManager.java:1525)
	at java.lang.ClassLoader$1.run(ClassLoader.java:503)
	at java.lang.ClassLoader$1.run(ClassLoader.java:501)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.lang.ClassLoader.checkPackageAccess(ClassLoader.java:501)
	at scala.concurrent.util.Unsafe.<clinit>(Unsafe.java:22)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:191)
	at com.google.appengine.tools.development.agent.runtime.RuntimeHelper.checkRestricted(RuntimeHelper.java:70)
	at com.google.appengine.tools.development.agent.runtime.Runtime.checkRestricted(Runtime.java:64)
	at scala.concurrent.forkjoin.ForkJoinPool.getUnsafe(ForkJoinPool.java:3757)
	at scala.concurrent.forkjoin.ForkJoinPool.<clinit>(ForkJoinPool.java:3681)
	at scala.concurrent.impl.ExecutionContextImpl.createExecutorService(ExecutionContextImpl.scala:77)
	at scala.concurrent.impl.ExecutionContextImpl.<init>(ExecutionContextImpl.scala:28)
	at scala.concurrent.ExecutionContext$Implicits$.global$lzycompute(ExecutionContext.scala:63)
	at scala.concurrent.ExecutionContext$Implicits$.global(ExecutionContext.scala:63)
	at dispatch.Defaults$.executor(defaults.scala:14)

scalaj-http (Scala)

import scalaj.http._

val response = Http("https://...")
  .postForm(Seq(
    "key" -> "value",
  )).asString

if (response.isSuccess) {
  PlainTextContent ~> ResponseString(response.body)
} else {
  NotFound
}
Caused by: java.lang.SecurityException: java.lang.IllegalAccessException: Reflection is not allowed on protected java.lang.String java.net.HttpURLConnection.method
	at com.google.appengine.runtime.Request.process-18f33f2754d7f312(Request.java)
	at java.lang.reflect.Field.setAccessible(Field.java:166)
	at scalaj.http.HttpOptions$.<init>(Http.scala:41)
	at scalaj.http.HttpOptions$.<clinit>(Http.scala)
	at scalaj.http.HttpConstants$.defaultOptions(Http.scala:497)
	at scalaj.http.BaseHttp$.$lessinit$greater$default$2(Http.scala:619)
	at scalaj.http.Http$.<init>(Http.scala:605)
	at scalaj.http.Http$.<clinit>(Http.scala)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment