Fork me on GitHub
#aws-lambda
<
2018-02-26
>
derpocious04:02:17

Hey, so great that there is a room for this! 🙂

derpocious04:02:12

I'm using the cljs-lambda starter with lein new cljs-lambda my-project, but I'm having trouble making any kind of http request

derpocious04:02:21

here's my code, just in 1 core.cljs file

derpocious04:02:34

(ns twitter-unfollower.core
  (:require [cljs-lambda.util :as lambda]
            [cljs-lambda.context :as ctx]
            [cljs-lambda.macros :refer-macros [deflambda]]
            [cljs-http.client :as http]
            [cljs.core.async :as async :refer [<!]])
  (:require-macros [cljs.core.async.macros :refer [go]]))

(defn doStuff []
  (println "sending request...")
  (go (let [response (<! (http/get ""
       {:with-credentials? false :query-params {"since" 2005}}))]
        (prn (:status response))
        (map :login (:body response)))))

(deflambda run-lambda []
  (doStuff))

derpocious04:02:24

and project.clj for leiningen

derpocious04:02:27

(defproject twitter-unfollower "0.1.0-SNAPSHOT"
  :description "Jim's bot"
  :url ""
  :dependencies [[org.clojure/clojure       "1.8.0"]
                 [org.clojure/clojurescript "1.8.51"]
                 [org.clojure/core.async    "0.2.395"]
                 [io.nervous/cljs-lambda    "0.3.5"]
                 [cljs-http "0.1.44"]]
  :plugins [[lein-cljsbuild "1.1.4"]
            [lein-npm       "0.6.0"]
            [lein-doo       "0.1.7"]
            [io.nervous/lein-cljs-lambda "0.6.6"]]
  :npm {:dependencies [[source-map-support "0.4.0"]]}
  :source-paths ["src"]
  :cljs-lambda
  {:defaults      {:role "arn:aws:iam::954459734159:role/cljs-lambda-default"}
   :resource-dirs ["static"]
   :functions
   [{:name   "cljs-twitter-unfollower"
     :invoke twitter-unfollower.core/run-lambda}]}
  :cljsbuild
  {:builds [{:id "twitter-unfollower"
             :source-paths ["src"]
             :compiler {:output-to     "target/twitter-unfollower/twitter_unfollower.js"
                        :output-dir    "target/twitter-unfollower"
                        :source-map    true
                        :target        :nodejs
                        :language-in   :ecmascript5
                        :optimizations :none}}
            {:id "twitter-unfollower-test"
             :source-paths ["src" "test"]
             :compiler {:output-to     "target/twitter-unfollower-test/twitter_unfollower.js"
                        :output-dir    "target/twitter-unfollower-test"
                        :target        :nodejs
                        :language-in   :ecmascript5
                        :optimizations :none
                        :main          twitter-unfollower.test-runner}}]})

derpocious04:02:21

But when I deploy this and run it I get an error. 😞

derpocious04:02:39

This as the response:

{
  "errorMessage": "RequestId: e61ddfc6-1aaa-11e8-853b-2f510d1b9f2d Process exited before completing request"
}

derpocious04:02:09

and this in the logs:

START RequestId: e61ddfc6-1aaa-11e8-853b-2f510d1b9f2d Version: $LATEST
2018-02-26T04:09:55.447Z	e61ddfc6-1aaa-11e8-853b-2f510d1b9f2d	sending request...
2018-02-26T04:09:55.888Z	e61ddfc6-1aaa-11e8-853b-2f510d1b9f2d	ReferenceError: XMLHttpRequest is not defined
    at [object Object].goog.net.DefaultXmlHttpFactory.createInstance (/var/task/target/twitter-unfollower/goog/net/xmlhttp.js:176:16)
    at Object.goog.net.XmlHttp (/var/task/target/twitter-unfollower/goog/net/xmlhttp.js:37:36)
    at [object Object].goog.net.XhrIo.createXhr (/var/task/target/twitter-unfollower/goog/net/xhrio.js:674:56)
    at [object Object].goog.net.XhrIo.send (/var/task/target/twitter-unfollower/goog/net/xhrio.js:522:20)
    at cljs_http$core$xhr (/var/task/target/twitter-unfollower/cljs_http/core.cljs:107:12)
    at cljs_http$core$request (/var/task/target/twitter-unfollower/cljs_http/core.cljs:150:6)
    at cljs_http.client.wrap_accept.cljs$core$IFn$_invoke$arity$variadic (/var/task/target/twitter-unfollower/cljs_http/client.cljs:118:8)
    at cljs_http.client.wrap_form_params (/var/task/target/twitter-unfollower/cljs_http/client.cljs:200:8)
    at cljs_http.client.wrap_multipart_params (/var/task/target/twitter-unfollower/cljs_http/client.cljs:216:8)
    at cljs_http.client.wrap_edn_params (/var/task/target/twitter-unfollower/cljs_http/client.cljs:97:8)
    at cljs_http.client.wrap_edn_response (/var/task/target/twitter-unfollower/cljs_http/client.cljs:104:22)
    at cljs_http.client.wrap_transit_params (/var/task/target/twitter-unfollower/cljs_http/client.cljs:151:8)
    at cljs_http.client.wrap_transit_response (/var/task/target/twitter-unfollower/cljs_http/client.cljs:162:24)
    at cljs_http.client.wrap_json_params (/var/task/target/twitter-unfollower/cljs_http/client.cljs:175:8)
    at cljs_http.client.wrap_json_response (/var/task/target/twitter-unfollower/cljs_http/client.cljs:182:22)
    at cljs_http.client.wrap_content_type.cljs$core$IFn$_invoke$arity$variadic (/var/task/target/twitter-unfollower/cljs_http/client.cljs:125:8)
    at cljs_http.client.wrap_query_params (/var/task/target/twitter-unfollower/cljs_http/client.cljs:187:8)
    at cljs_http.client.wrap_basic_auth.cljs$core$IFn$_invoke$arity$variadic (/var/task/target/twitter-unfollower/cljs_http/client.cljs:245:10)
    at cljs_http.client.wrap_oauth (/var/task/target/twitter-unfollower/cljs_http/client.cljs:255:8)
    at cljs_http.client.wrap_method (/var/task/target/twitter-unfollower/cljs_http/client.cljs:221:8)
    at cljs_http.client.wrap_url (/var/task/target/twitter-unfollower/cljs_http/client.cljs:231:8)
    at cljs_http.client.wrap_channel_from_request_map (/var/task/target/twitter-unfollower/cljs_http/client.cljs:264:8)
END RequestId: e61ddfc6-1aaa-11e8-853b-2f510d1b9f2d
REPORT RequestId: e61ddfc6-1aaa-11e8-853b-2f510d1b9f2d	Duration: 546.73 ms	Billed Duration: 600 ms 	Memory Size: 128 MB	Max Memory Used: 84 MB	
RequestId: e61ddfc6-1aaa-11e8-853b-2f510d1b9f2d Process exited before completing request

gonewest81805:02:14

I don’t have a lot of javascript experience, but I thought XMLHttpRequest was not provided in node.js… only in browsers.

lloydshark10:02:04

Yeah, think you are right, looks like this issue.

derpocious13:02:42

thanks @lloydshark!

derpocious13:02:17

This is probably a dumb question, but how can I add this library to a lein project? I tried to put [xh2r "0.1.4"] under the vector in project.clj :npm {:dependencies []}, but I am getting this error:

derpocious13:02:55

npm ERR! Darwin 17.4.0
npm ERR! argv "/Users/jameslynch/.nvm/versions/node/v6.9.5/bin/node" "/Users/jameslynch/.nvm/versions/node/v6.9.5/bin/npm" "install"
npm ERR! node v6.9.5
npm ERR! npm  v3.10.10
npm ERR! code E404

npm ERR! 404 Registry returned 404 for GET on 
npm ERR! 404 
npm ERR! 404  'xh2r' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404 It was specified as a dependency of 'twitter-unfollower'
npm ERR! 404 
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! Please include the following file with any support request:

valtteri14:02:00

It’s xhr2, not xh2r. You have a typo

derpocious14:02:43

Oh (face-palm)