0

I have a python client side code which hits an URL with compressed JSON data.

I want to decompress and print the JSON data (that i got from client request) in java.

Client code:

  #!/usr/bin/python

  import sys, getopt
  import requests
  import json
  from zlib import compress

  s = requests.session()
  url = "http://1.1.1.1:8080/eventfull/send/data/"
  payload = dict(
    username="test",
    password="test123",
    emailid=sys.argv[1],
    campaignfrom="info@newsletter.x.com",
    send_id="1234",
    istest="1",
    render_id=sys.argv[2],
    subject="Eventfull :: Services HeartBeat",
    htmlbody="<html><body><p>Hi Team,</p></br></br><p>This is a Test Campaign to ensure eventfull calls are working as expected</p></br></br><p>Thanks,</p><br><p>Tech Team</p></body></html>",
    textbody="Testing"
  )
  json_string = json.dumps(payload)
  compressed = compress(json_string,9)
  response = s.post(url, data=compressed )

  print response.status_code
  print response.content
Shajal Ahamed
  • 141
  • 2
  • 16
Aamir Khan
  • 101
  • 1
  • 7

2 Answers2

0

since you're using zlib to compress the data, you could either use Gzip or InflaterInputStream to decompress the data, other stackoverflow users faced a problem similar to yours and you can find a way to decompress as gzip here and for the InflaterInputStream here , a little google search for zlib in java came up with this library as well.

user1
  • 254
  • 1
  • 15
  • I've got this code earlier. Firstly, I need to get the request body ( which I don't know) and then I can decompress. Thanks a lot! – Aamir Khan Apr 24 '18 at 07:10
0

Thank you all for your help, through which i could able to complete my task. It might took so long to complete but worth it. Below is my working code. [Worth sharing]

@POST  
@Path("/Data")  
@Consumes("application/zip")


public Response uploadFile2(InputStream incomingData) {




    try {
        //Decompressing...
        InflaterInputStream inf = new InflaterInputStream(incomingData);

        //Extracting Json Data....
        JSONParser jsonParser = new JSONParser();
        JSONObject jsonObject = (JSONObject)jsonParser.parse(
                new InputStreamReader(inf, "UTF-8"));


        String name = (String) jsonObject.get("username");
    }
    catch(SQLException se){
        System.out.println(se.getLocalizedMessage());
    }


    catch (Exception e) {
        e.printStackTrace();
    }


    // return HTTP response 200 in case of success
    return Response.status(200).entity("Success").build();
Aamir Khan
  • 101
  • 1
  • 7