Implement custom run for uploaded files

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
Nic0Nic0Nii 3 years ago
parent 51aca794b0
commit 61c628c344
  1. 6
      src/sig/client.java
  2. 62
      src/sig/requests/POSTRequest.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<String>)res.run()).body());
*/
/*
//Regular POST request with body:
POSTRequest postRes = new POSTRequest("https://postman-echo.com/post","Test body");
System.out.println(((HttpResponse<String>)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<String>)postRes.run()).body());*/
POSTRequest postRes = new POSTRequest("https://postman-echo.com/post",Path.of(".gitignore"));
System.out.println(((HttpResponse<String>)postRes.run()).body());
} catch (FailedResponseException e) {
e.printStackTrace();
}

@ -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<String>(){
@Override
public int statusCode() {
return 0;
}
@Override
public HttpRequest request() {
return null;
}
@Override
public Optional<HttpResponse<String>> previousResponse() {
return null;
}
@Override
public HttpHeaders headers() {
return null;
}
@Override
public String body() {
return "Response";
}
@Override
public Optional<SSLSession> 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{

Loading…
Cancel
Save