I need to send data from my web interface to linux server. I am using tomcat as server. I am new to java, i visited many questions but didn't find any exact solution. Other than code i would like to see help regarding process/logic to send data on linux server. Manual i post data on server like this, that i need to post by web interface throuhg HTTP.
curl --header "Content-type: application/json" --request POST --data '{"name":"John", "id":"500", "employee":"yes","salary":"5000","dept":"accounts"}' http://serverNumericURL.com
I'd recommend using a library like apache http components
Example:
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://serverNumericURL.com");
httpPost.setEntity(new StringRequestEntity(jsonString, "application/json", "UTF-8"));
httpClient.executeMethod(httpPost);
Or if you would like to stick to the JDK, you could use the HTTPURLConnection as in the following link which covers both HTTP GET and POST.
http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/
Related
I have a my Wordpress site, installed by Bitnami in a AWS cloud server. Let's assume that the site is www.example.com .
I have also a Java application that sends http request (GET and POST) to my site, in order to exchange some infos (I use in Java: Apache HttpClient library ver. 4.5).
I implemented in the website the php code able to receive some specific http request, coming from my java app.
When the Java app sends an http request GET, it works.
The problem is with http request POST.
Let's consider for example the following http request POST:
https://example.com/wp-json/v1/meth-post?param=whatever
When I send that from my browser (I use a developper feature of FireFox) it works.
When I send exactly the same http request POST from my Java App I get the error message n. 301 from the website.
I saw googling this message means that the page was moved permantly.
Java code used to do the http request POST:
final StringEntity OGGETTO_StringEntity = new StringEntity(JSON_STRING, ContentType.APPLICATION_JSON);
final HttpPost OGGETTO_HttpPost = new HttpPost("https://example.com/wp-json/v1/meth-post?param=whatever");
OGGETTO_HttpPost.setEntity(OGGETTO_StringEntity);
RESPONSE = OGGETTO_HttpClient.execute(OGGETTO_HttpPost);
Inside the .../wordpress/htdocs/.htaccess file I have:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I really cannot undertand why I have that error if the http request POST is exactly the same from browser and java app.
Thank you in Advance
I have an android app, which should execute some specific commands on an other computer where spotify is running as desktop app.
The current state is, that i have a blank activity with a button. If the button is pressed, my mobile phone with the android app should execute this command from spotify web api:
https://developer.spotify.com/console/put-pause/
the command is like:
curl -X "PUT" "https://api.spotify.com/v1/me/player/pause" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
I'm trying out different things and ways to solve that now for the whole day and nothing works.
Has anyone a similar problem or some tips how to do it?
Thank you!
The mentioned API endpoint does work for me on the web console as well as curl request on my desktop. If this does not work for you, please check if your authorization token does include the scope user-modify-playback-state.
Regarding tips: The Spotify libraries for authorization and playback control are pretty easy to integrate.
I do not know exactly if the playback control part is only for the playback on your device (mobile phone) or does include your use case to control the playback on your desktop. If not, you could implement this as a simple OkHttp request:
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://api.spotify.com/v1/me/player/pause")
.put(null)
.addHeader("Authorization", "Bearer xxxx-xxxx-xxxx…")
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.build();
Response response = client.newCall(request).execute();
If this does not help, it would be good to know some more details from your attempts of calling this api, like the actual call in Java or responses/ errors.
Please be aware that player pause/play in spotify API is a premium feature.
{
"error" : {
"status" : 403,
"message" : "Player command failed: Premium required",
"reason" : "PREMIUM_REQUIRED"
}
}
I would like to perform an HTTP POST of a Binary file, as I would from Postman like this:
Note here I have selected HTTP Post, with the binary option, and flagged my file "splop.sar".
The server replies http 409 which is expected for my use case.
My attempt in Jenkins has been as follows, using the httpRequest plugin :
def myFile = readFile("splop.sar")
httpRequest authentication: 'userdef', consoleLogResponseBody: true, contentType: 'APPLICATION_ZIP', httpMode: 'POST', requestBody: myFile, responseHandle: 'NONE', url: 'myurl.com/service'
The problem is, my backend server rejects the request as invalid, no doubt the encoding has failed here. I have tried alternatives such as:
def myFile = readFile("splop.sar").bytes
Which also fails, as well as:
def myFile = readFile("splop.sar").bytes.toString()
Which also fails!
What can I do within Jenkins to provide the same style of HTTP Post that postman is giving from within my pipeline? I believe the big difference here is what makes the POST request a 'binary' post, against some other form?
I don't think you can do it with http-request-plugin, there's an open bug for that.
You could definitely do that with curl - Send POST Request with Data Specified in File via Curl
curl -i -X POST host:port/post-file \
-H "Content-Type: text/xml" \
--data-binary "#path/to/file"
The httpRequest plugin has got a parameter uploadFile which is a path to the file you want to upload. See documentation https://www.jenkins.io/doc/pipeline/steps/http_request/
(Available since release 1.14 which is quite new)
I am able to execute the below nifi commands for retreiving system-diagnostics metrics. Is there anyway that these commands can be executed through java code by invoking nifi api using our own methods as we do in aws cloudwatch to put metrics.
read USER
read -s PASS
TOKEN=`curl -X POST --data "username=$USER&password=$PASS" -k https://nifiHostName.com:nifiPort/nifi-api/access/token`
curl -H "Authorization: Bearer $TOKEN" -k https://nifiHostName:nifiPort/nifi-api/system-diagnostics\ | python -m json.tool
If above is not possible can anyone answer how to give Authorization: Bearer $TOKEN through a simple Java code that would be great.
Or else by using simple https client is it possible to execute these commands through java. If yes could you please help me on this.
You can invoke the Apache NiFi API through any Java HTTP interaction. For example, you could use the standard URL object and execute getConnection() and openConnection() operations against it (see an extensive answer describing this process). Calling openConnection() returns a URLConnection object, on which you set headers using connection.setRequestProperty("Header name", value).
You can also use a library like jersey-client, Apache HttpClient, or OkHTTP.
Any one of these methods will allow you to set an HTTP header with the Authorization: Bearer value.
I am using to call a url from command prompt of curl. The url contains post data, user name and password. Added to that the response should be a file if authenticates or shows generally 401 response. I have been trying to find out the solution for the last 2 hours, but cant able to do that. For example i want to send two values(x,y) using post and with that i am sending the username and password also, say for instance
curl -u test#test.com:test http://www.example.com/mappingFile
if i sending like this i am getting
<html><head><title>Error 405 Request method 'GET' not supported</title></head>
<body><h2>Error 405 Request method 'GET' not supported</h2></body>
</html>
Please any body can help me.
You are not posting any data with the curl command and hence it is going as a GET instead of POST.
Here is how you can send the POST request using --data option:
curl --data "dummyparam=dummvalue" -u test#test.com:test http://www.example.com/mappingFile
In your case you send the data via GET.
use curl --request POST to send data via POST.
The data you can add with the option --data.
curl -d "appid=1234567" --header "username:test#test.com" --header "password:test" >> output.apk http://example.com/fileName.apk