Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics

Dashboard
Notifications
Mark all as read
Q&A

Trying to create a POST request with Apache

+3
−0

I have tried many different ways to do this with apache but the server seems to not be receiving the data. Stacktraces are not being printed so I can only assume I have the request set up wrong for the server to receive the data. My current code looks like this:

try {
    fixUntrustCertificate();
    url = new URL("https://panel.<address>.com/api/v1/server/send_command? 
    token=" + apikey + "&id=6");
    CloseableHttpClient client = HttpClients.createDefault();

    //POST to be executed
    HttpPost post = new HttpPost("https://panel. 
    <address>.com/api/v1/server/send_command?token=" + apikey + "&id=6");


    //data to send in POST
    List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
    params.add(new BasicNameValuePair("command", command)); 
    //command is a console command such as "ping", which would warrant 
    //"pong" in response from the server.


    //Headers
    post.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; 
    x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.192 
    Safari/537.36");
    post.setHeader("Accept", "text/html");
    post.setHeader("Host", "panel.<address>.com");
    post.setHeader("Content-Type", "multipart/form-data");

    //Entity to send
    post.setEntity(new UrlEncodedFormEntity(params));

    //POST execution
    client.execute(post);
    client.close();

} catch (MalformedURLException e) {
    String stack = ExceptionUtils.getStackTrace(e);
    stackTrace = stack;
} catch (IOException e) {
    String stack = ExceptionUtils.getStackTrace(e);
    stackTrace = stack;
} catch (JSONException e) {
    String stack = ExceptionUtils.getStackTrace(e);
    stackTrace = stack;
} catch (NoSuchAlgorithmException e) {
        String stack = ExceptionUtils.getStackTrace(e);
    stackTrace = stack;
} catch (KeyManagementException e) {
    String stack = ExceptionUtils.getStackTrace(e);
    stackTrace = stack;
}
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

3 comments

Do you receive a response from the server? Typically servers provide a response, except for the case when the connection cannot be made (timeout, explicit connection reset from the server). I think there is no exception, but an HTTP code that should be available if you read the response from the line client.execute(post). Alexei‭ 25 days ago

Did you check the Apache server logs to confirm whether the server is even receiving the request itself? What happens when you access it directly from your browser - even if it's a GET instead of a POST? Under Ubuntu, they'd be under /var/log/apache2/{access | error}-log by default. ghost-in-the-zsh‭ 25 days ago

What makes you think that the "server seems to not receive the data"? meriton‭ 23 days ago

0 answers

Sign up to answer this question »