From 61c628c344c94825a4c37740146a9f7826cfcc05 Mon Sep 17 00:00:00 2001 From: Nic0Nic0Nii Date: Fri, 20 May 2022 18:52:27 +0000 Subject: [PATCH] Implement custom run for uploaded files Co-authored-by: sigonasr2 --- src/sig/client.java | 6 +++ src/sig/requests/POSTRequest.java | 62 ++++++++++++++++++++++++------- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/src/sig/client.java b/src/sig/client.java index d530c31..3b6528b 100644 --- a/src/sig/client.java +++ b/src/sig/client.java @@ -1,5 +1,6 @@ package sig; import java.net.http.HttpResponse; +import java.nio.file.Path; import sig.exceptions.FailedResponseException; import sig.requests.GETRequest; @@ -36,14 +37,19 @@ public class client { System.out.println(((HttpResponse)res.run()).body()); */ + /* //Regular POST request with body: POSTRequest postRes = new POSTRequest("https://postman-echo.com/post","Test body"); System.out.println(((HttpResponse)postRes.run()).body()); //POST request with body and headers: postRes = new POSTRequest("https://postman-echo.com/post","Test body", 30000, "header1","value1", "header2","value2"); + System.out.println(((HttpResponse)postRes.run()).body());*/ + + POSTRequest postRes = new POSTRequest("https://postman-echo.com/post",Path.of(".gitignore")); System.out.println(((HttpResponse)postRes.run()).body()); + } catch (FailedResponseException e) { e.printStackTrace(); } diff --git a/src/sig/requests/POSTRequest.java b/src/sig/requests/POSTRequest.java index 7b808fe..5fdaaf9 100644 --- a/src/sig/requests/POSTRequest.java +++ b/src/sig/requests/POSTRequest.java @@ -1,22 +1,16 @@ package sig.requests; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.net.HttpURLConnection; -import java.net.URL; -import java.net.URLConnection; +import java.net.URI; +import java.net.http.HttpHeaders; import java.net.http.HttpRequest; +import java.net.http.HttpResponse; import java.net.http.HttpClient.Builder; +import java.net.http.HttpClient.Version; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; +import java.util.Optional; + +import javax.net.ssl.SSLSession; import sig.exceptions.FailedResponseException; @@ -49,7 +43,47 @@ public class POSTRequest extends GETRequest{ super(url); this.uploadFile=uploadFile; } - + @Override + public HttpResponse run() throws FailedResponseException { + if (uploadFile!=null) { + return new HttpResponse(){ + @Override + public int statusCode() { + return 0; + } + @Override + public HttpRequest request() { + return null; + } + @Override + public Optional> previousResponse() { + return null; + } + @Override + public HttpHeaders headers() { + return null; + } + @Override + public String body() { + return "Response"; + } + @Override + public Optional sslSession() { + return null; + } + @Override + public URI uri() { + return null; + } + @Override + public Version version() { + return null; + } + }; + } else { + return super.run(); + } + } @Override protected java.net.http.HttpRequest.Builder finalizeRequestPreBuild( java.net.http.HttpRequest.Builder requestBuild) throws FailedResponseException{