Last active
March 15, 2020 14:07
-
-
Save rtazaki/b4661e8426e05bdb7c590b25f0d1e6ad to your computer and use it in GitHub Desktop.
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
group 'org.example' | |
version '1.0-SNAPSHOT' | |
buildscript { | |
ext { | |
kotlin_version = 'latest.integration' | |
} | |
repositories { | |
jcenter() | |
} | |
dependencies { | |
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" | |
} | |
} | |
apply plugin: 'kotlin' | |
apply plugin: 'application' | |
mainClassName = 'MainKt' | |
repositories { | |
jcenter() | |
} | |
allprojects { | |
repositories { | |
maven { | |
url "https://jitpack.io" | |
} | |
} | |
} | |
dependencies { | |
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" | |
// implementation "khttp:khttp:latest.integration" | |
implementation "com.github.rtazaki:khttp:master-SNAPSHOT" | |
implementation "com.github.kittinunf.fuel:fuel:latest.integration" | |
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:latest.integration" | |
} |
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
from flask import Flask, request, jsonify | |
app = Flask(__name__) | |
app.config["JSON_AS_ASCII"] = False | |
@app.route("/test", methods=["POST"]) | |
def test(): | |
if request.method == "POST": | |
content_type = request.headers["Content-Type"] | |
if content_type != "application/json": | |
print(content_type) | |
return jsonify({"cause": "ヘッダがJsonじゃない(content type error)"}), 400 | |
data = request.get_json() | |
print(data) | |
return data | |
@app.route("/contact", methods=["GET"]) | |
def contact(): | |
if request.method == "GET": | |
contents = request.args.get("value", "") | |
print(contents) | |
return contents | |
if __name__ == "__main__": | |
app.run(debug=True, host="example_path", port=50000) |
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
127.0.0.1 example_path | |
127.0.0.1 example-path | |
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
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | |
import com.github.kittinunf.fuel.httpPost | |
fun main() { | |
// khttpだと、怒られた | |
// => java.net.URISyntaxException: Illegal character in hostname at index 14: http://example_path:50000/test | |
// => ローカルで試した対策版でFixしている。→プルリク済み。 | |
try { | |
val post = khttp.post("http://example_path:50000/test", | |
json = mapOf("name" to "rtazaki", "hobby" to "syogi")) | |
println(post) | |
// 追試: 日本語GET成功 | |
val get = khttp.get("http://example_path:50000/contact", | |
params = mapOf("value" to "日本語")) | |
println(get) | |
} catch (e: Exception) { | |
println(e) | |
} | |
// // fuelだとどうかな? → 問題ない。 | |
// val mapper = jacksonObjectMapper() | |
// try { | |
// val url = "http://example_path:50000/test" | |
// val ret = url.httpPost() | |
// .header(mapOf("Content-Type" to "application/json")) | |
// .body(mapper.writeValueAsString(mapOf("name" to "rtazaki", "hobby" to "syogi"))) | |
// .response() | |
// println(ret) | |
// } catch (e: Exception) { | |
// println(e) | |
// } | |
// 原因: | |
// khttpだと、 | |
// ->GenericRequest.kt->URL.toIDN()->return URL(URI(.. | |
// で、URIでアンダーバーが使えないため、 | |
// java.net.URISyntaxException: Illegal character | |
// となってしまうため。 | |
// JAVAだと絶対にダメではない。 | |
// fuelでは「URLをURIに」変換して回避している? | |
// [参考] | |
// https://stackoverflow.com/questions/28568188/java-net-uri-get-host-with-underscores | |
// try { | |
// val xx = "http://coder_1.tanglei.name" | |
// val u = URI(xx) | |
// println(u) | |
// println(u.host) // null | |
// val url = URL(xx) | |
// println(url) | |
// println(url.host) // coder_1.tanglei.name | |
// val u2 = URI(url.protocol, url.host, url.path, "") | |
// println(u2) | |
// } catch (e: Exception) { | |
// println("exception got: " + e.message) | |
// } | |
// => exception got: Illegal character in hostname at index 12: http://coder_1.tanglei.name# | |
// 回避策 | |
// val uriObj = URI("https://pmi_artifacts_prod.s3.amazonaws.com") | |
// if (uriObj.host == null) { | |
// val hostField = URI::class.java.getDeclaredField("host") | |
// hostField.isAccessible = true | |
// hostField.set(uriObj, "pmi_artifacts_prod.s3.amazonaws.com") | |
// } | |
// println(uriObj.host) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment