diff --git a/distribution/std-lib/Standard/src/Base/Network/Http.enso b/distribution/std-lib/Standard/src/Base/Network/Http.enso index 1c76cd64ec5c..2f088d9f8f63 100644 --- a/distribution/std-lib/Standard/src/Base/Network/Http.enso +++ b/distribution/std-lib/Standard/src/Base/Network/Http.enso @@ -399,7 +399,7 @@ type Http example_post_json = json = Json.parse '{"key":"val"}' Examples.http_client.post_json "http://httpbin.org/post" json - post_json : (Text | Uri) -> Json -> Vector.Vector -> Response ! Request_Error + post_json : (Text | Uri) -> (Text | Json) -> Vector.Vector -> Response ! Request_Error post_json uri body_json (headers = []) = new_headers = [Header.application_json] req = Request.post uri (Request_Body.Json body_json) headers . with_headers new_headers @@ -443,7 +443,7 @@ type Http example_post_json = json = Json.parse '{"key":"val"}' Examples.http_client.put_json "http://httpbin.org/post" json - put_json : (Text | Uri) -> Json -> Vector.Vector -> Response ! Request_Error + put_json : (Text | Uri) -> (Text | Json) -> Vector.Vector -> Response ! Request_Error put_json uri body_json (headers = []) = new_headers = [Header.application_json] req = Request.put uri (Request_Body.Json body_json) headers . with_headers new_headers @@ -562,7 +562,8 @@ type Http Pair req (body_publishers.ofString text) Request_Body.Json json -> builder.header Header.application_json.name Header.application_json.value - Pair req (body_publishers.ofString json.to_text) + json_body = if json.is_a Text then json else json.to_text + Pair req (body_publishers.ofString json_body) Request_Body.Form form -> add_multipart form = body_builder = Http_Utils.multipart_body_builder @@ -603,7 +604,7 @@ type Http Response.response (this.internal_http_client.send http_request body_handler) response.catch e-> case e of Polyglot_Error err -> - Error.throw (Request_Error err.getMessage) + Error.throw (Request_Error err.getClass.getSimpleName err.getMessage) _ -> Error.throw e @@ -646,11 +647,15 @@ type Http An error when sending an Http request. Arguments: + - error_type: The type of the error. - message: The message for the error. -type Request_Error message +type Request_Error error_type message ## UNSTABLE Convert a request error to a human-readable form. -Request_Error.to_display_text = "Error when sending request: " + this.message - +Request_Error.to_display_text = + description_text = case this.message of + Nothing -> "" + _ -> " " + this.message + this.error_type + " error when sending request." + description_text diff --git a/distribution/std-lib/Standard/src/Base/Network/Http/Request.enso b/distribution/std-lib/Standard/src/Base/Network/Http/Request.enso index 9957a49f66c7..2c71fd5fd0c1 100644 --- a/distribution/std-lib/Standard/src/Base/Network/Http/Request.enso +++ b/distribution/std-lib/Standard/src/Base/Network/Http/Request.enso @@ -195,7 +195,7 @@ type Request import Standard.Base.Network.Uri example_with_body = - Request.post (Uri.parse "http://example.com") Request_Body.Empty |> .with_body Request_Body.Empty + Request.post (Uri.parse "http://example.com") Request_Body.Empty |> _.with_body Request_Body.Empty with_body : Request_Body -> Request with_body new_body = Request this.method this.uri this.headers new_body @@ -213,8 +213,8 @@ type Request import Standard.Base.Network.Uri example_with_json = - Request.post (Uri.parse "http://example.com") Request_Body.Empty |> .with_body "{ "a": "b" }" - with_json : Text -> Request + Request.post (Uri.parse "http://example.com") Request_Body.Empty |> _.with_json '{ "a": "b" }' + with_json : (Text | Json) -> Request with_json json_body = new_body = Request_Body.Json json_body Request this.method this.uri this.headers new_body . with_headers [Header.application_json] diff --git a/distribution/std-lib/Standard/src/Base/Network/Uri.enso b/distribution/std-lib/Standard/src/Base/Network/Uri.enso index 2aa93e68f9a6..7e2e2a2e2c2f 100644 --- a/distribution/std-lib/Standard/src/Base/Network/Uri.enso +++ b/distribution/std-lib/Standard/src/Base/Network/Uri.enso @@ -200,7 +200,7 @@ type Uri example_to_json = Examples.uri.to_json to_json : Json.String - to_json : Json.String this.to_text + to_json = Json.String this.to_text ## Check if this URI is equal to another URI. diff --git a/test/Tests/src/Network/Http_Spec.enso b/test/Tests/src/Network/Http_Spec.enso index 1901273438d5..660d8af7a27f 100644 --- a/test/Tests/src/Network/Http_Spec.enso +++ b/test/Tests/src/Network/Http_Spec.enso @@ -5,6 +5,7 @@ import Standard.Base.Data.Time.Duration import Standard.Base.Network.Http import Standard.Base.Network.Http.Form import Standard.Base.Network.Http.Header +import Standard.Base.Network.Http.Method import Standard.Base.Network.Http.Request import Standard.Base.Network.Http.Request.Body as Request_Body import Standard.Base.Network.Http.Status_Code @@ -37,6 +38,7 @@ spec = http.version.should_equal version_setting Test.specify "should throw error when requesting invalid Uri" <| Http.new.get "not a uri" . should_fail_with Uri.Syntax_Error + Test.specify "should send Get request" <| expected_response = Json.parse <| ''' { @@ -65,6 +67,7 @@ spec = res = Http.get "http://localhost:8080/get" res.code.should_equal Status_Code.ok res.body.to_json.should_equal expected_response + Test.specify "should fetch the body of a Get request" <| expected_response = Json.parse <| ''' { @@ -80,10 +83,12 @@ spec = res.to_json.should_equal expected_response Test.specify "should return error if the fetch method fails" <| Http.fetch "http://undefined_host" . should_fail_with Http.Request_Error + Test.specify "should send Head request" <| res = Http.new.head "http://localhost:8080/get" res.code.should_equal Status_Code.ok res.body.to_text.should_equal '' + Test.specify "should Post empty body" <| expected_response = Json.parse <| ''' { @@ -258,3 +263,69 @@ spec = res = Http.new.post "http://localhost:8080/post" body_bytes res.code.should_equal Status_Code.ok res.body.to_json.should_equal expected_response + + Test.specify "should create and send Get request" <| + expected_response = Json.parse <| ''' + { + "headers": { + "Content-Length": "0", + "User-Agent": "Java-http-client/11.0.10" + }, + "origin": "127.0.0.1", + "url": "", + "args": {} + } + req = Request.new Method.Get "http://localhost:8080/get" + res = Http.new.request req + res.code.should_equal Status_Code.ok + res.body.to_json.should_equal expected_response + Test.specify "should create and send Post request with json body" <| + expected_response = Json.parse <| ''' + { + "headers": { + "Content-Length": "13", + "Content-Type": "application/json", + "User-Agent": "Java-http-client/11.0.10" + }, + "origin": "127.0.0.1", + "url": "", + "args": {}, + "data": "{\\"key\\":\\"val\\"}", + "files": null, + "form": null, + "json": { + "key": "val" + } + } + json_body = Json.parse <| ''' + { "key": "val" } + req = Request.new Method.Post "http://localhost:8080/post" + req_with_body = req.with_json json_body + res = Http.new.request req_with_body + res.code.should_equal Status_Code.ok + res.body.to_json.should_equal expected_response + Test.specify "should create and send Post request with json text" <| + expected_response = Json.parse <| ''' + { + "headers": { + "Content-Length": "16", + "Content-Type": "application/json", + "User-Agent": "Java-http-client/11.0.10" + }, + "origin": "127.0.0.1", + "url": "", + "args": {}, + "data": "{ \\"key\\": \\"val\\" }", + "files": null, + "form": null, + "json": { + "key": "val" + } + } + json_text = ''' + { "key": "val" } + req = Request.new Method.Post "http://localhost:8080/post" + req_with_body = req.with_json json_text + res = Http.new.request req_with_body + res.code.should_equal Status_Code.ok + res.body.to_json.should_equal expected_response