0

I have a simple python code working but I am unable to get it to work in Java it also works via curl and postman. Please help

The following code is python and is fairly simple and straightforward. it returns 200.

<!-- language: lang-python -->

import requests
params = (
    ('member', 'xxx'),
)

response = requests.post('http://jenkinsurl1/submitRemoveMember', params=params, auth=('user', 'notbase64encodedtoken'))
print(response)

Returns 200

The following code is in java and I am unable to find a simple and straightforward way to do this in java.

<!-- language: lang-java -->

//main() function

String auth = "user" + ":" + "notbase64encodedtoken";
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
final String POST_PARAMS = "member=xxxx";
MyPOSTRequest(POST_PARAMS,encodedAuth,"http://jenkinsurl1/submitRemoveMember");


public static void MyPOSTRequest(String Parameters, byte[] encodedAuth, String POST_URL) throws IOException {

URL obj = new URL(POST_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
byte[] postData       = Parameters.getBytes( StandardCharsets.UTF_8 );
int    postDataLength = postData.length;
con.setRequestMethod("POST");
con.setDoOutput( true );
con.setInstanceFollowRedirects( false );
con.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty( "charset", "utf-8");
con.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
String authHeaderValue = "Basic " + new String(encodedAuth);
con.setRequestProperty("Authorization", authHeaderValue);
con.setUseCaches( false );
    
try( DataOutputStream wr = new DataOutputStream( con.getOutputStream())) {
    wr.write( postData );
    wr.flush();
}
int responseCode = con.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);
    
if (responseCode == HttpURLConnection.HTTP_OK) { //success
  BufferedReader in = new BufferedReader(new InputStreamReader(
  con.getInputStream()));
  String inputLine;
  StringBuffer response = new StringBuffer();
    
   while ((inputLine = in.readLine()) != null) {
     response.append(inputLine);
    }
    in.close();
    
     // print result
     System.out.println(response.toString());
     } else {
       System.out.println("POST request not worked");
       }
 }

POST Response Code :: 302
POST request not worked

HumayunM
  • 514
  • 4
  • 12
  • If you can use a 3rd party (apache) : https://stackoverflow.com/questions/3324717/sending-http-post-request-in-java – limido Aug 12 '20 at 10:48
  • I suggest to print body of response. It may contain explanation on why request is failed. – talex Aug 12 '20 at 11:02
  • So the Java code is configured to not follow redirects `(con.setInstanceFollowRedirects( false )`. And you did receive HTTP status code 302 which is a redirect. Maybe your python code followed the redirect, but the Java code didn't? – Matteus Hemström Aug 12 '20 at 14:56

2 Answers2

0

Your Java code doesn't process redirect response (HTTP code 302).

talex
  • 17,973
  • 3
  • 29
  • 66
0

This was more to do with Jenkins idiosyncrasies than to do with java. The 302 error code was expected and Jenkins accepts and does the work required and returns with 302. Although I don't know how python deals with it internally (and calls two times?) and returns code 200 eventually

FYI if setInstanceFollowedRedirects is set to true, I get a 403

jenkins bug
similar unanswered on Stackoverflow

This is how I went around it. Posting for others who might run into it.

 int responseCode = con.getResponseCode();
 System.out.println("POST Response Code :: " + responseCode);

 if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP) { //MOVED_TEMP
  String location = con.getHeaderField("Location");
  System.out.println(location);
  MyPOSTRequest(Parameters, encodedAuth, location); //calling the same function again with redirected url.
 }

POST Response Code :: 302
http://jenkinsurl1
POST Response Code :: 200

HumayunM
  • 514
  • 4
  • 12