Arul
Arul

Reputation: 1179

HttpUrlConnection with post request and parameter as JSON object?

The same question asked here but I want to make a request with following structure.

"method": "post",

"url": parserApiUrl,

"data": {

"url": s3BaseUrl + imageKey

},

"headers": {

"x-api-key": "sdsdasdwdw"

}

Upvotes: 1

Views: 7586

Answers (1)

Arul
Arul

Reputation: 1179

I Found a solution for this by this steps

urlConnection = (HttpURLConnection) url
                        .openConnection();
                urlConnection.setDoOutput(true);
                urlConnection.setRequestMethod("POST");
                urlConnection.setRequestProperty("Content-Type", "application/json");
                urlConnection.setRequestProperty("x-api-key", x_api);
                urlConnection.setRequestProperty("Accept", "application/json");

                String datajson =  "{\"file\": \""+imageString.trim()+"\"}";
                Log.e("data","json:"+datajson);

                OutputStream os = urlConnection.getOutputStream();
                os.write(datajson.getBytes("UTF-8"));
                os.close();

Upvotes: 7

Related Questions