PUT/POST using java is not working in webservice API in Bugzilla

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

PUT/POST using java is not working in webservice API in Bugzilla

devteam
PUT/POST using java is not working in webservice API in Bugzilla

I am trying to put/post a data in Bugzilla through a java code by using the webservice APIs(rest version : 5.0.4) of Bugzilla.

While running the following code its giving an error code 404 and not doing the put/post.

I have tried to authenticate using the username/password and also using the api_key but unable to do the put/post.

Please provide some help to resolve the issue


public class Example{
public static String token;
//public static String api_key="**apikey**";
public static void main(String args[]) throws IOException {
   // String POST_PARAMS = "{\n" + "\"id\":10,\r\n" +
     //"\"version\":\"V1.1\"" + "\n}";

//String POST_PARAMS =  "{\"id\":10,\"version\":\"V1.1\"}";



   String urlString=null;

 urlString= "http://bug**lla.Example**.com/bugzilla/rest/login?login=usernbame&password=pwd";
   
        URL url = new URL(urlString);

        HttpURLConnection urlConnection= (HttpURLConnection) url.openConnection();
        urlConnection.setRequestProperty("Accept","application/json");
        urlConnection.setRequestMethod("GET");
   
        urlConnection.connect();
        System.out.println(urlString);
       BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

    String line;

    while ((line = br.readLine()) != null)
             {
         
                System.out.println(line);                                
                                                       
                JSONObject jsonObject = new JSONObject(line);

                 //System.out.println(jsonObject.get("token"));
                 token = (String) jsonObject.get("token");    
                System.out.println("token:" +token);        
             }

   URL obj = new URL("http://bug*lla.Example**.com/bugzilla/rest/bug/?api_key="+api_key);
   System.out.println("link:"+obj);

    HttpURLConnection postConnection = (HttpURLConnection) obj.openConnection();
    postConnection.setDoOutput(true);  
    postConnection.setRequestMethod("PUT");
  //postConnection.setRequestProperty("username","api_key;**api*key123");
 
   postConnection.setRequestProperty("username","api_key;token");
    postConnection.setRequestProperty("Content-Type", "application/json");
    postConnection.setRequestProperty("charset", "utf-8");
   
   
    OutputStream os = postConnection.getOutputStream();
    os.write(POST_PARAMS.getBytes());
    os.flush();
    os.close();
    int responseCode = postConnection.getResponseCode();
    System.out.println("POST Response Code :  " + responseCode);
    System.out.println("POST Response Message : " + postConnection.getResponseMessage());
    if (responseCode == HttpURLConnection.HTTP_CREATED) { //success
        BufferedReader in = new BufferedReader(new InputStreamReader(
            postConnection.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 NOT WORKED");
    }
}
}
_______________________________________________
support-bugzilla mailing list
[hidden email]
https://lists.mozilla.org/listinfo/support-bugzilla
PLEASE put [hidden email] in the To: field when you reply.