0

I have been trying to implement custom request method in HTTP header while posting my data to the server URL. My application specific URL accepts -X parameter and -d for the data. Basically I am trying to dump JSON data into my influx DB using CURL command which is working fine. But the issue is, if I am implementing the same in java with proper approach, it is not supported or working.

My CURL command is :

curl -X POST -d 'my_json_data' 'my_url'

How can I implement the same in java using HttpUrlConnection or other available approach i.e- Apache Client service.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • What do you mean by "custom request method"? Your curl command is a regular POST request. – Stefan Bartel Jun 26 '15 at 14:06
  • According to CURL documentation, -X is "(HTTP) Specifies a custom request method to use when communicating with the HTTP server. The specified request will be used instead of the method otherwise used (which defaults to GET). " My server accept data using -X option, along with -d. – Swapnil Shailesh Srivastawa Jun 26 '15 at 15:00
  • You are misunderstanding the curl doc. -d sets the data for a http post request therefore curl will automatically create a post request if it is set. -X allows you set the request method (the header value that is) to a different value. But since you set it to POST it is of course still a post request. – Stefan Bartel Jun 26 '15 at 15:21
  • What do you mean by "My server accept data using -X option, along with -d." I am getting the bad feeling you trying to do something like this: `http://my_url?X=POST&d=my_json_data` – Stefan Bartel Jun 26 '15 at 15:27
  • No. What I am trying to do is, curl -X POST -d "mydata" "myurl" , this is the format which I need to follow to dump my JSON data into influxdb directly. – Swapnil Shailesh Srivastawa Jun 27 '15 at 06:01
  • So indeed you want to create a simple post request in java. – Stefan Bartel Jun 29 '15 at 11:47

1 Answers1

0

Based on the documentation provided here, you may use the following outline to solve your purpose:

// Use URI builder to build the URL and connect to URL using HttpURLConnection
Uri uri = Uri.parse(BASE_URL).buildUpon().appendQueryParameter("d","my_json_data");

// Create the request to your API and open the connection
URL url = new URL(uri.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.connect();

// Read input in string
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
    return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
    buffer.append(line + "\n");
}
if (buffer.length() == 0) {
    return null;
}
String response = buffer.toString();

I will also recommend you to go through this discussion: How to add parameters to HttpURLConnection using POST

Community
  • 1
  • 1
Bhoot
  • 2,614
  • 1
  • 19
  • 36
  • My connection format is in this way- curl -X POST -d 'my_json_data' 'my_url'. I need to maintain this exact format anyhow, because my server will listen to this exact format only. Need to send -X and -d with the header. – Swapnil Shailesh Srivastawa Jun 26 '15 at 15:09