From fd43413fb82bfefd938e5686438b54caf99b5672 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Tue, 2 Aug 2022 19:51:45 -0500 Subject: [PATCH] Allow for x-www-form-urlencoded whatever Co-authored-by: sigonasr2 --- .gitignore | 3 +- src/sig/client.java | 54 +++++++++++++++++++++++++++++-- src/sig/requests/POSTRequest.java | 7 +++- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 5241a72..2c110ee 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -*.class \ No newline at end of file +*.class +.clientsecret diff --git a/src/sig/client.java b/src/sig/client.java index 97b480f..a76621e 100644 --- a/src/sig/client.java +++ b/src/sig/client.java @@ -1,18 +1,59 @@ package sig; +import java.io.IOException; +import java.net.URLEncoder; import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Path; +import java.util.HashMap; import sig.exceptions.FailedResponseException; import sig.requests.GETRequest; import sig.requests.POSTRequest; public class client { + static String SECRET = ""; + + private static String JSON(HashMap testMap) { + StringBuilder sb = new StringBuilder(); + String temp = testMap.toString(); + if (temp.charAt(0)=='{') { + sb.append("{"); + int marker=1; + boolean ending=false; + while (marker)res.run()).body()); + //GETRequest res = new GETRequest("https://postman-echo.com/get?foo1=bar1&foo2=bar2"); + //System.out.println(((HttpResponse)res.run()).body()); /*GET Block @@ -36,9 +77,16 @@ public class client { res = new GETRequest("https://postman-echo.com/basic-auth","postman","password",30000,null); System.out.println(((HttpResponse)res.run()).body()); */ + /*GETRequest res = new GETRequest("https://api.twitch.tv/helix/users?login=sigonitori",30000,"Client-Id","otppg8l1x7xbrfnqex1np1qba47mzf"); + System.out.println(((HttpResponse)res.run()).body());*/ + HashMap obj = new HashMap(); + obj.put("type","stream.online"); + obj.put("version","1"); + obj.put("version","1"); //Regular POST request with body: - POSTRequest postRes = new POSTRequest("https://8080-sigonasr2-sigplace-5j5fknii87n.ws-us54.gitpod.io/","Test a longer body\nwith much more\nlines now.\n\n"); + POSTRequest postRes = new POSTRequest("https://id.twitch.tv/oauth2/token","client_id=otppg8l1x7xbrfnqex1np1qba47mzf&client_secret="+SECRET+"&grant_type=client_credentials"); + postRes.setContentType("application/x-www-form-urlencoded"); System.out.println(((HttpResponse)postRes.run()).body()); /* diff --git a/src/sig/requests/POSTRequest.java b/src/sig/requests/POSTRequest.java index fc792e7..c70ce4e 100644 --- a/src/sig/requests/POSTRequest.java +++ b/src/sig/requests/POSTRequest.java @@ -22,6 +22,11 @@ public class POSTRequest extends GETRequest{ String body = ""; Path uploadFile = null; + String contentType = "application/json"; + + public void setContentType(String contentType) { + this.contentType = contentType; + } public POSTRequest(String url, String body, String username, String password, long timeout, Path outputFile, String... headers) { super(url, username, password, timeout, outputFile, headers); @@ -111,7 +116,7 @@ public class POSTRequest extends GETRequest{ @Override protected java.net.http.HttpRequest.Builder finalizeRequestPreBuild( java.net.http.HttpRequest.Builder requestBuild) throws FailedResponseException{ - requestBuild.headers("Content-Type","application/json"); + requestBuild.headers("Content-Type",contentType); try { return file!=null?requestBuild.POST(HttpRequest.BodyPublishers.ofFile(file)): body.length()>0?requestBuild.POST(HttpRequest.BodyPublishers.ofString(body)):