Created
July 20, 2018 18:54
-
-
Save wibisono/5ebd814ba0b8df86cb66d6fe781245f7 to your computer and use it in GitHub Desktop.
Scala-request issue 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\u001b[32mimport \u001b[39m\u001b[36m$ivy.$ \n", | |
"\u001b[39m" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import $ivy.`com.lihaoyi::requests:0.1.2`\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Start a local netcat: \n", | |
"* Mac OS: nc -l 8899\n", | |
"* Linux: nc -l -p 8899\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\u001b[36msession\u001b[39m: \u001b[32mrequests\u001b[39m.\u001b[32mSession\u001b[39m = \u001b[33mSession\u001b[39m(\n", | |
" \u001b[33mMap\u001b[39m(\n", | |
" \u001b[32m\"User-Agent\"\u001b[39m -> \u001b[32m\"requests-scala\"\u001b[39m,\n", | |
" \u001b[32m\"Accept-Encoding\"\u001b[39m -> \u001b[32m\"gzip, deflate\"\u001b[39m,\n", | |
" \u001b[32m\"Connection\"\u001b[39m -> \u001b[32m\"keep-alive\"\u001b[39m,\n", | |
" \u001b[32m\"Accept\"\u001b[39m -> \u001b[32m\"*/*\"\u001b[39m\n", | |
" ),\n", | |
" \u001b[33mMap\u001b[39m(),\n", | |
" \u001b[33mMap\u001b[39m(),\n", | |
" requests.RequestAuth$Empty$@2c0a64bf,\n", | |
" null,\n", | |
" \u001b[32mtrue\u001b[39m,\n", | |
"\u001b[33m...\u001b[39m\n", | |
"\u001b[36mheaders\u001b[39m: \u001b[32mMap\u001b[39m[\u001b[32mString\u001b[39m, \u001b[32mString\u001b[39m] = \u001b[33mMap\u001b[39m(\u001b[32m\"Content-Type\"\u001b[39m -> \u001b[32m\"application/x-www-form-urlencoded\"\u001b[39m)\n", | |
"\u001b[36murlPost\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"http://localhost:8899/post-test\"\u001b[39m\n", | |
"\u001b[36mpayload\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"somePayload=value\"\u001b[39m" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val session = requests.Session()\n", | |
"val headers = Map(\n", | |
" \"Content-Type\" -> \"application/x-www-form-urlencoded\")\n", | |
"\n", | |
"val urlPost = \"http://localhost:8899/post-test\"\n", | |
"val payload = \"somePayload=value\"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"ename": "", | |
"evalue": "", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[31mjava.net.SocketException: Unexpected end of file from server\u001b[39m", | |
" sun.net.www.http.HttpClient.parseHTTPHeader(\u001b[32mHttpClient.java\u001b[39m:\u001b[32m792\u001b[39m)", | |
" sun.net.www.http.HttpClient.parseHTTP(\u001b[32mHttpClient.java\u001b[39m:\u001b[32m647\u001b[39m)", | |
" sun.net.www.protocol.http.HttpURLConnection.getInputStream0(\u001b[32mHttpURLConnection.java\u001b[39m:\u001b[32m1536\u001b[39m)", | |
" sun.net.www.protocol.http.HttpURLConnection.getInputStream(\u001b[32mHttpURLConnection.java\u001b[39m:\u001b[32m1441\u001b[39m)", | |
" java.net.HttpURLConnection.getResponseCode(\u001b[32mHttpURLConnection.java\u001b[39m:\u001b[32m480\u001b[39m)", | |
" requests.Requester.liftedTree1$1(\u001b[32mRequester.scala\u001b[39m:\u001b[32m264\u001b[39m)", | |
" requests.Requester.stream(\u001b[32mRequester.scala\u001b[39m:\u001b[32m263\u001b[39m)", | |
" requests.Requester.apply(\u001b[32mRequester.scala\u001b[39m:\u001b[32m95\u001b[39m)", | |
" $sess.cmd6Wrapper$Helper.<init>(\u001b[32mcmd6.sc\u001b[39m:\u001b[32m1\u001b[39m)", | |
" $sess.cmd6Wrapper.<init>(\u001b[32mcmd6.sc\u001b[39m:\u001b[32m108\u001b[39m)", | |
" $sess.cmd6$.<init>(\u001b[32mcmd6.sc\u001b[39m:\u001b[32m87\u001b[39m)", | |
" $sess.cmd6$.<clinit>(\u001b[32mcmd6.sc\u001b[39m:\u001b[32m-1\u001b[39m)" | |
] | |
} | |
], | |
"source": [ | |
"val response = session.post(urlPost, data=payload, headers=headers)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Verify the expected output in the netcat console:\n", | |
"\n", | |
"```\n", | |
"POST /post-test HTTP/1.1\n", | |
"User-Agent: requests-scala\n", | |
"Accept-Encoding: gzip, deflate\n", | |
"Accept: */*\n", | |
"Content-Type: application/x-www-form-urlencoded\n", | |
"Cookie:\n", | |
"Cache-Control: no-cache\n", | |
"Pragma: no-cache\n", | |
"Host: localhost:8899\n", | |
"Connection: keep-alive\n", | |
"Content-Length: 17\n", | |
"\n", | |
"somePayload=value\n", | |
"```\n", | |
"\n", | |
"Start another netcat" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": false, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [ | |
{ | |
"ename": "", | |
"evalue": "", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[31mrequests.TimeoutException: Request to http://localhost:8899/get-test timed out. (readTimeout: 10000, connectTimout: 10000)\u001b[39m", | |
" requests.Requester.liftedTree1$1(\u001b[32mRequester.scala\u001b[39m:\u001b[32m271\u001b[39m)", | |
" requests.Requester.stream(\u001b[32mRequester.scala\u001b[39m:\u001b[32m263\u001b[39m)", | |
" requests.Requester.apply(\u001b[32mRequester.scala\u001b[39m:\u001b[32m95\u001b[39m)", | |
" $sess.cmd7Wrapper$Helper.<init>(\u001b[32mcmd7.sc\u001b[39m:\u001b[32m7\u001b[39m)", | |
" $sess.cmd7Wrapper.<init>(\u001b[32mcmd7.sc\u001b[39m:\u001b[32m120\u001b[39m)", | |
" $sess.cmd7$.<init>(\u001b[32mcmd7.sc\u001b[39m:\u001b[32m87\u001b[39m)", | |
" $sess.cmd7$.<clinit>(\u001b[32mcmd7.sc\u001b[39m:\u001b[32m-1\u001b[39m)" | |
] | |
} | |
], | |
"source": [ | |
"val urlGet = \"http://localhost:8899/get-test\"\n", | |
" \n", | |
"val querystring = Map(\"query\" -> \"value\")\n", | |
" \n", | |
"val headers2 = Map(\"Cache-Control\" -> \"no-cache\")\n", | |
" \n", | |
"val response2 = session.get(urlGet, data=payload, headers=headers2, params=querystring)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"collapsed": true, | |
"deletable": true, | |
"editable": true | |
}, | |
"source": [ | |
"Unfortunately even when you called `session.get` it sent a POST:\n", | |
"\n", | |
"```\n", | |
"POST /get-test?query=value HTTP/1.1\n", | |
"User-Agent: requests-scala\n", | |
"Accept-Encoding: gzip, deflate\n", | |
"Accept: */*\n", | |
"Content-Type: text/plain\n", | |
"Cache-Control: no-cache\n", | |
"Cookie:\n", | |
"Pragma: no-cache\n", | |
"Host: localhost:8899\n", | |
"Connection: keep-alive\n", | |
"Content-Length: 17\n", | |
"\n", | |
"somePayload=value%\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Scala", | |
"language": "scala", | |
"name": "scala" | |
}, | |
"language_info": { | |
"codemirror_mode": "text/x-scala", | |
"file_extension": ".scala", | |
"mimetype": "text/x-scala", | |
"name": "scala211", | |
"nbconvert_exporter": "script", | |
"pygments_lexer": "scala", | |
"version": "2.11.11" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment