Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public String apiMethod_POST()
{
String foutput = "";
try
{
URL url = new URL(uri);
System.out.println("API URL : " + url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
if(authToken!=null && authToken.length()>0)
{
conn.setRequestProperty("X-Auth-Token",authToken);
}
System.out.println("JSON sent in input is .........>>>>>>>" + json);
OutputStream os = conn.getOutputStream();
os.write(json.getBytes());
os.flush();
int Responsecode = conn.getResponseCode();
String ResponseCode=String.valueOf(Responsecode);
String ResponseMessage = conn.getResponseMessage();
String LoginContentType = conn.getContentType();
if (conn.getResponseCode() != 200)
{
throw new RuntimeException("Failed : HTTP Error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output ;
while ((output = br.readLine()) != null)
{
foutput = foutput + output;
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return foutput;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.