Post operation in bugzilla using java code

I am trying to post a data in Bugzilla through a java code by using the APIs of Bugzilla. 
While running the code its getting an error code and mentioning need to login before accessing this part. I have tried to authenticate but that cannot be combined along with this post operation. 

I am attaching the code :


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**.**/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**.**/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");
    }
}
}
0
devteam
1/12/2019 12:28:36 AM
mozilla.support.bugzilla 10029 articles. 0 followers. Post Follow

5 Replies
8 Views

Similar Articles

[PageSpeed] 59

Guten Tag devteam@cloudplusinfotech.com,
am Samstag, 12. Januar 2019 um 01:28 schrieben Sie:

> I am trying to post a data in Bugzilla through a java code by using the A=
PIs of Bugzilla.

You should provide more details about the version of Bugzilla you are
using, especially because the web-API changed in the latest major
versions.

> While running the code its getting an error code and mentioning
> need to login before accessing this part. I have tried to
> authenticate but that cannot be combined along with this post operation.

Does your GET-example work and are you getting a token? Your
PUT-example seems wrong to me, you are not providing a JSON-object as
the one and only request body, but GET-parameters and HTTP-headers
instead.

> The REST API only supports JSON input, and either JSON or JSONP
> output. So objects sent and received must be in JSON format.

https://bugzilla.readthedocs.io/en/5.0/api/core/v1/general.html#basic-infor=
mation

You need to create some JSON-object containing all your data and PUT
or POST that as the request body to the web service. No GET-params or
additional headers. You can't combine username and API-key as well, so
keep an I on that you have the individual values correct.

https://bugzilla.readthedocs.io/en/5.0/api/core/v1/general.html#authenticat=
ion

Mit freundlichen Gr=FC=DFen,

Thorsten Sch=F6ning

--=20
Thorsten Sch=F6ning       E-Mail: Thorsten.Schoening@AM-SoFT.de
AM-SoFT IT-Systeme      http://www.AM-SoFT.de/

Telefon...........05151-  9468- 55
Fax...............05151-  9468- 88
Mobil..............0178-8 9468- 04

AM-SoFT GmbH IT-Systeme, Brandenburger Str. 7c, 31789 Hameln
AG Hannover HRB 207 694 - Gesch=E4ftsf=FChrer: Andreas Muchow

0
windows
1/12/2019 8:48:23 AM
Bugzilla version is 5.0.4

I am using the following code but receiving the error 400 bad request. I ha=
ve not been able to find out why it is not working.=20

In the PUT section I have only used JSON object as the request body.=20
The GET example using token and api_key are both working.

I have used username and password for the PUT request.
 Please find the code and error message bellow

import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class Sample {
public static String token=3D"xxxxxxxxxx";

    public static String result;


    public static void main(String args[]) {
String urlString1=3Dnull;
try {
     =20
            JSONObject data =3D new JSONObject();
      =20
            data.put("status", "IN_PROGRESS");

            URL url =3D new URL("http://bugzilla.testserver.com/bugzilla/re=
st/bug/14");
            HttpURLConnection httpConnection  =3D (HttpURLConnection) url.o=
penConnection();
            httpConnection.setDoOutput(true);
            httpConnection.setRequestMethod("PUT");
            httpConnection.setRequestProperty("example@sample.com","pwd");
            httpConnection.setRequestProperty("Content-Type", "application/=
json");
            httpConnection.setRequestProperty("Accept", "application/json")=
;
         =20
            DataOutputStream wr =3D new DataOutputStream(httpConnection.get=
OutputStream());
            wr.write(data.toString().getBytes());
            Integer responseCode =3D httpConnection.getResponseCode();

            BufferedReader bufferedReader;

            if (responseCode > 199 && responseCode < 300) {
                bufferedReader =3D new BufferedReader(new InputStreamReader=
(httpConnection.getInputStream()));
            } else {
                bufferedReader =3D new BufferedReader(new InputStreamReader=
(httpConnection.getErrorStream()));
            }

            StringBuilder content =3D new StringBuilder();
            String line;
            while ((line =3D bufferedReader.readLine()) !=3D null) {
                content.append(line).append("\n");
            }
            bufferedReader.close();

            System.out.println(content.toString());

        } catch (Exception e) {
            System.out.println("Error Message");
            System.out.println(e.getClass().getSimpleName());
            System.out.println(e.getMessage());
        }
    }

}

Error Message:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.7 (Ubuntu) Server at localhost Port 80</address>
</body></html>



On Saturday, January 12, 2019 at 2:18:35 PM UTC+5:30, Thorsten Sch=C3=B6nin=
g wrote:
> Guten Tag devteam@cloudplusinfotech.com,
> am Samstag, 12. Januar 2019 um 01:28 schrieben Sie:
>=20
> > I am trying to post a data in Bugzilla through a java code by using the=
 APIs of Bugzilla.
>=20
> You should provide more details about the version of Bugzilla you are
> using, especially because the web-API changed in the latest major
> versions.
>=20
> > While running the code its getting an error code and mentioning
> > need to login before accessing this part. I have tried to
> > authenticate but that cannot be combined along with this post operation=
..
>=20
> Does your GET-example work and are you getting a token? Your
> PUT-example seems wrong to me, you are not providing a JSON-object as
> the one and only request body, but GET-parameters and HTTP-headers
> instead.
>=20
> > The REST API only supports JSON input, and either JSON or JSONP
> > output. So objects sent and received must be in JSON format.
>=20
> https://bugzilla.readthedocs.io/en/5.0/api/core/v1/general.html#basic-inf=
ormation
>=20
> You need to create some JSON-object containing all your data and PUT
> or POST that as the request body to the web service. No GET-params or
> additional headers. You can't combine username and API-key as well, so
> keep an I on that you have the individual values correct.
>=20
> https://bugzilla.readthedocs.io/en/5.0/api/core/v1/general.html#authentic=
ation
>=20
> Mit freundlichen Gr=C3=BC=C3=9Fen,
>=20
> Thorsten Sch=C3=B6ning
>=20
> --=20
> Thorsten Sch=C3=B6ning       E-Mail: Thorsten.Schoening@AM-SoFT.de
> AM-SoFT IT-Systeme      http://www.AM-SoFT.de/
>=20
> Telefon...........05151-  9468- 55
> Fax...............05151-  9468- 88
> Mobil..............0178-8 9468- 04
>=20
> AM-SoFT GmbH IT-Systeme, Brandenburger Str. 7c, 31789 Hameln
> AG Hannover HRB 207 694 - Gesch=C3=A4ftsf=C3=BChrer: Andreas Muchow

0
sruthi
1/15/2019 8:51:45 AM
Guten Tag sruthi.s@cloudplusinfotech.com,
am Dienstag, 15. Januar 2019 um 09:51 schrieben Sie:

> I am using the following code but receiving the error 400 bad
> request. I have not been able to find out why it is not working.=20

Use Wireshark or such to have a look at the request you actually send,
it might still really be wrong for some reason. For example, all
examples I find are closing the OutputStream after writing data to it,
which you don't seem to do. It might be that your request is not
properly formatted because of that, as HttpURLConnection can't know
when your body is actually finished. Remember that HTTP-requests can
be rendered differently chunked vs. non-chunked etc.

https://stackoverflow.com/questions/20020902/android-httpurlconnection-how-=
to-set-post-data-in-http-body
http://guruparang.blogspot.com/2016/01/example-on-working-with-json-and.html

> httpConnection.setRequestProperty("example@sample.com","pwd");

That is most likely wrong and might or might not harm.

>             DataOutputStream wr =3D new
> DataOutputStream(httpConnection.getOutputStream());
>             wr.write(data.toString().getBytes());
>             Integer responseCode =3D httpConnection.getResponseCode();

No "wr.close()", which might or might not be wrong.

Mit freundlichen Gr=C3=BC=C3=9Fen,

Thorsten Sch=C3=B6ning

--=20
Thorsten Sch=C3=B6ning       E-Mail: Thorsten.Schoening@AM-SoFT.de
AM-SoFT IT-Systeme      http://www.AM-SoFT.de/

Telefon...........05151-  9468- 55
Fax...............05151-  9468- 88
Mobil..............0178-8 9468- 04

AM-SoFT GmbH IT-Systeme, Brandenburger Str. 7c, 31789 Hameln
AG Hannover HRB 207 694 - Gesch=C3=A4ftsf=C3=BChrer: Andreas Muchow

0
utf
1/15/2019 9:21:18 AM
Thank you for the mail

> > httpConnection.setRequestProperty("example@sample.com","pwd");
The above statement is used for authentication.
=20
If we remove that part from the code then , it shows an error:
{"documentation":"https://bugzilla.readthedocs.org/en/5.0/api/","error":tru=
e,"message":"You must log in before using this part of Bugzilla.","code":41=
0}

If we use that part it shows another error as following:=20
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.7 (Ubuntu) Server at localhost Port 80</address>
</body></html>

We did the packet capturing, following is the output;

*******************************
07:05:22.614926 IP (tos 0x0, ttl 105, id 26456, offset 0, flags [DF], proto=
 TCP (6), length 285)
    xx.xx.xx.93.57573 > 10.1.0.5.http: Flags [P.], cksum 0xc53b (correct), =
seq 1:246, ack 1, win 259, length 245: HTTP, length: 245
        GET /bugzilla/rest/valid_login?login=3Dlogin@cloudplusinfotech.com&=
api_key=3Dkeyvalue HTTP/1.1
        Accept: application/json
        User-Agent: Java/1.8.0_121
        Host: host1.cloudplusinfotech.com
        Connection: keep-alive

07:05:22.614958 IP (tos 0x0, ttl 64, id 1840, offset 0, flags [DF], proto T=
CP (6), length 40)
    10.1.0.5.http > xx.xx.xx.93.57573: Flags [.], cksum 0xf54c (incorrect -=
> 0x8a4d), ack 246, win 237, length 0
07:05:23.250185 IP (tos 0x0, ttl 64, id 1841, offset 0, flags [DF], proto T=
CP (6), length 513)
    10.1.0.5.http > xx.xx.xx.93.57573: Flags [P.], cksum 0xf725 (incorrect =
-> 0x0e3e), seq 1:474, ack 246, win 237, length 473: HTTP, length: 473
        HTTP/1.1 200 OK
        Date: Thu, 17 Jan 2019 07:05:22 GMT
        Server: Apache/2.4.7 (Ubuntu)
        X-xss-protection: 1; mode=3Dblock
        X-frame-options: SAMEORIGIN
        Access-control-allow-headers: origin, content-type, accept, x-reque=
sted-with
        X-content-type-options: nosniff
        Access-control-allow-origin: *
        Etag: 3UEGMp182VEiVeT1RV05tw
        Keep-Alive: timeout=3D5, max=3D100
        Connection: Keep-Alive
        Transfer-Encoding: chunked
        Content-Type: application/json; charset=3DUTF-8

        f
        {"result":true}
07:05:23.259441 IP (tos 0x0, ttl 64, id 1842, offset 0, flags [DF], proto T=
CP (6), length 45)
    10.1.0.5.http > xx.xx.xx.93.57573: Flags [P.], cksum 0xf551 (incorrect =
-> 0x444d), seq 474:479, ack 246, win 237, length 5: HTTP
07:05:23.479793 IP (tos 0x0, ttl 105, id 26457, offset 0, flags [DF], proto=
 TCP (6), length 40)
    xx.xx.xx.93.57573 > 10.1.0.5.http: Flags [.], cksum 0x885b (correct), a=
ck 479, win 257, length 0
07:05:23.497932 IP (tos 0x0, ttl 105, id 26458, offset 0, flags [DF], proto=
 TCP (6), length 289)
    xx.xx.xx.93.57573 > 10.1.0.5.http: Flags [P.], cksum 0x3053 (correct), =
seq 246:495, ack 479, win 257, length 249: HTTP, length: 249
        PUT /bugzilla/rest/bug/14 HTTP/1.1
        login@cloudplusinfotech.com: pwd
        Content-Type: application/json
        Accept: application/json
        User-Agent: Java/1.8.0_121
        Host: host1.cloudplusinfotech.com
        Connection: keep-alive
        Content-Length: 24

07:05:23.497947 IP (tos 0x0, ttl 64, id 1843, offset 0, flags [DF], proto T=
CP (6), length 40)
    10.1.0.5.http > xx.xx.xx.93.57573: Flags [.], cksum 0xf54c (incorrect -=
> 0x876e), ack 495, win 245, length 0
07:05:23.498092 IP (tos 0x0, ttl 105, id 26459, offset 0, flags [DF], proto=
 TCP (6), length 64)
    xx.xx.xx.93.57573 > 10.1.0.5.http: Flags [P.], cksum 0xc731 (correct), =
seq 495:519, ack 479, win 257, length 24: HTTP
07:05:23.498105 IP (tos 0x0, ttl 64, id 1844, offset 0, flags [DF], proto T=
CP (6), length 40)
    10.1.0.5.http > xx.xx.xx.93.57573: Flags [.], cksum 0xf54c (incorrect -=
> 0x8756), ack 519, win 245, length 0
07:05:23.498287 IP (tos 0x0, ttl 64, id 1845, offset 0, flags [DF], proto T=
CP (6), length 521)
    10.1.0.5.http > xx.xx.xx.93.57573: Flags [P.], cksum 0xf72d (incorrect =
-> 0x5490), seq 479:960, ack 519, win 245, length 481: HTTP, length: 481
        HTTP/1.1 400 Bad Request
        Date: Thu, 17 Jan 2019 07:05:23 GMT
        Server: Apache/2.4.7 (Ubuntu)
        Content-Length: 300
        Connection: close
        Content-Type: text/html; charset=3Diso-8859-1

        <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
        <html><head>
        <title>400 Bad Request</title>
        </head><body>
        <h1>Bad Request</h1>
        <p>Your browser sent a request that this server could not understan=
d.<br />
        </p>
        <hr>
        <address>Apache/2.4.7 (Ubuntu) Server at localhost Port 80</address=
>
        </body></html>
07:05:23.498377 IP (tos 0x0, ttl 64, id 1846, offset 0, flags [DF], proto T=
CP (6), length 40)
    10.1.0.5.http > xx.xx.xx.93.57573: Flags [F.], cksum 0xf54c (incorrect =
-> 0x8574), seq 960, ack 519, win 245, length 0
07:05:23.731174 IP (tos 0x0, ttl 104, id 26460, offset 0, flags [DF], proto=
 TCP (6), length 40)
    xx.xx.xx.93.57573 > 10.1.0.5.http: Flags [F.], cksum 0x856a (correct), =
seq 519, ack 960, win 255, length 0
07:05:23.731196 IP (tos 0x0, ttl 64, id 1847, offset 0, flags [DF], proto T=
CP (6), length 40)
    10.1.0.5.http > xx.xx.xx.93.57573: Flags [.], cksum 0xf54c (incorrect -=
> 0x8573), ack 520, win 245, length 0
07:05:23.742355 IP (tos 0x0, ttl 104, id 26461, offset 0, flags [DF], proto=
 TCP (6), length 40)
    xx.xx.xx.93.57573 > 10.1.0.5.http: Flags [.], cksum 0x8569 (correct), a=
ck 961, win 255, length 0
*********************




On Tuesday, January 15, 2019 at 2:51:31 PM UTC+5:30, Thorsten Sch=C3=B6ning=
 wrote:
> Guten Tag sruthi.s@cloudplusinfotech.com,
> am Dienstag, 15. Januar 2019 um 09:51 schrieben Sie:
>=20
> > I am using the following code but receiving the error 400 bad
> > request. I have not been able to find out why it is not working.=20
>=20
> Use Wireshark or such to have a look at the request you actually send,
> it might still really be wrong for some reason. For example, all
> examples I find are closing the OutputStream after writing data to it,
> which you don't seem to do. It might be that your request is not
> properly formatted because of that, as HttpURLConnection can't know
> when your body is actually finished. Remember that HTTP-requests can
> be rendered differently chunked vs. non-chunked etc.
>=20
> https://stackoverflow.com/questions/20020902/android-httpurlconnection-ho=
w-to-set-post-data-in-http-body
> http://guruparang.blogspot.com/2016/01/example-on-working-with-json-and.h=
tml
>=20
> > httpConnection.setRequestProperty("example@sample.com","pwd");
>=20
> That is most likely wrong and might or might not harm.
>=20
> >             DataOutputStream wr =3D new
> > DataOutputStream(httpConnection.getOutputStream());
> >             wr.write(data.toString().getBytes());
> >             Integer responseCode =3D httpConnection.getResponseCode();
>=20
> No "wr.close()", which might or might not be wrong.
>=20
> Mit freundlichen Gr=C3=BC=C3=9Fen,
>=20
> Thorsten Sch=C3=B6ning
>=20
> --=20
> Thorsten Sch=C3=B6ning       E-Mail: Thorsten.Schoening@AM-SoFT.de
> AM-SoFT IT-Systeme      http://www.AM-SoFT.de/
>=20
> Telefon...........05151-  9468- 55
> Fax...............05151-  9468- 88
> Mobil..............0178-8 9468- 04
>=20
> AM-SoFT GmbH IT-Systeme, Brandenburger Str. 7c, 31789 Hameln
> AG Hannover HRB 207 694 - Gesch=C3=A4ftsf=C3=BChrer: Andreas Muchow

0
devteam
1/17/2019 7:15:11 AM
Guten Tag devteam@cloudplusinfotech.com,
am Donnerstag, 17. Januar 2019 um 08:15 schrieben Sie:

>> > httpConnection.setRequestProperty("example@sample.com","pwd");
> The above statement is used for authentication.

I was just wondering because the docs don't seem to mention that one
can set arbitrary request headers and Bugzilla will take them as
username and password.

https://bugzilla.readthedocs.io/en/5.0/api/core/v1/general.html#authenticat=
ion

> If we remove that part from the code then , it shows an error:
> {"documentation":"https://bugzilla.readthedocs.org/en/5.0/api/","error":t=
rue,"message":"You
> must log in before using this part of Bugzilla.","code":410}

As your request is wrong anyway, that might simply be coincidence.

> 07:05:23.497932 IP (tos 0x0, ttl 105, id 26458, offset 0, flags [DF], pro=
to TCP (6), length 289)
>     xx.xx.xx.93.57573 > 10.1.0.5.http: Flags [P.], cksum 0x3053
> (correct), seq 246:495, ack 479, win 257, length 249: HTTP, length: 249
>         PUT /bugzilla/rest/bug/14 HTTP/1.1
>         login@cloudplusinfotech.com: pwd
>         Content-Type: application/json
>         Accept: application/json
>         User-Agent: Java/1.8.0_121
>         Host: host1.cloudplusinfotech.com
>         Connection: keep-alive
>         Content-Length: 24

> 07:05:23.497947 IP (tos 0x0, ttl 64, id 1843, offset 0, flags [DF], proto=
 TCP (6), length 40)
>     10.1.0.5.http > xx.xx.xx.93.57573: Flags [.], cksum 0xf54c
[...]

You didn't tell if you actually changed anything regarding closing the
stream for the body or not.

Looking at the captured request, I guess you didn't, because the body
is missing. The formerly captured response contained a body
({"result":true}), so I don't think it's just missing from your
provided output, but you really don't send it. 24 bytes would fit to a
JSON object ({"status":"IN_PROGRESS"}), so I guess the request is
simply not rendered entirely because you are not closing the stream
containing the body.

Mit freundlichen Gr=C3=BC=C3=9Fen,

Thorsten Sch=C3=B6ning

--=20
Thorsten Sch=C3=B6ning       E-Mail: Thorsten.Schoening@AM-SoFT.de
AM-SoFT IT-Systeme      http://www.AM-SoFT.de/

Telefon...........05151-  9468- 55
Fax...............05151-  9468- 88
Mobil..............0178-8 9468- 04

AM-SoFT GmbH IT-Systeme, Brandenburger Str. 7c, 31789 Hameln
AG Hannover HRB 207 694 - Gesch=C3=A4ftsf=C3=BChrer: Andreas Muchow

0
utf
1/17/2019 8:17:27 AM
Reply: