I am trying to access Mindsphere URL with Java Code. I am getting 403 forbidden error while doing it. While I am able to hit other POST URL's for other sites, Mindsphere URL is getting blocked by same piece of Java Code. Can someone help?
What am i missing in my Code?
restTemplate.exchange(,,*,TimeseriesData.class) is line giving error
MindSphere demands a authorization header with a JWT Token, if you call directly the API. I guess you have an Developer account in MindSphere. Try Application credentials in the Developer cockpit. With that credentials you can get a bearer token with an oauth flow.
If not just ping me again.
See Exampel with OK HTTP
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials");
Request request = new Request.Builder()
.url("https://questdev.piam.eu1.mindsphere.io/oauth/token")
.post(body)
.addHeader("Accept", "application/json")
.addHeader("cache-control", "no-cache,no-cache")
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.addHeader("Postman-Token", "24126d6b-3461-48fb-9060-6fd005804227")
.build();
Response response = client.newCall(request).execute();
Related
I have a nodejs web server running and I want to a body to the request in my java program.
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.GET()
.header( "Content-Type", "text/plain;charset=UTF-8")
.uri(URI.create("http://localhost:3000"))
.POST(HttpRequest.BodyPublishers.ofString("Hello"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("status:" + response.statusCode());
System.out.println("response:" + response.body());
If I delete this line: .POST(HttpRequest.BodyPublishers.ofString("Hello")) everything works fine, I get the response from the server, but there is no body to the request.
With this line I get status code 404 and the body of the response to the client is:
How can I add body to the request?
First of all try understand differences between GET and POST HTTP methods
https://www.w3schools.com/tags/ref_httpmethods.asp
There is also a good example in docs: https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpRequest.BodyPublishers.html
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://example.com/"))
.header("Content-Type", "text/plain; charset=UTF-8")
.POST(BodyPublishers.ofString("some body text"))
.build();
You can also look here as well
https://www.baeldung.com/java-9-http-client
I am trying to Authenticate Oauth 1.0 in android.
I send a request with postman and get a response via postman and this program gives me a Java code but it doesn't work.its return 401 error
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://test.com/wp-json/wc/v3/customers?
oauth_consumer_key=KEY&oauth_token=&oauth_signature_method=HMAC-HA1&oauth_timestamp=1564471240&oauth_nonce=iEo45PESRdt&oauth_version=1.0&oauth_signature=NQR4Xr5OKlb3H+rL0y2PNLdfXpY=")
.get()
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.addHeader("User-Agent", "PostmanRuntime/7.15.2")
.addHeader("Accept", "*/*")
.addHeader("Cache-Control", "no-cache")
.addHeader("Postman-Token", "6fb2e16e-376c-453a-bf30-18ef6c00020c,c14db88a-55dd-4287-a132-8ca441a5e3f0")
.addHeader("Host", "test.com")
.addHeader("Accept-Encoding", "gzip, deflate")
.addHeader("Connection", "keep-alive")
.addHeader("cache-control", "no-cache")
.build();
Response response = client.newCall(request).execute();
Seems like code generated by postman does not respect Authentication section.
Go to Headers section and click "9 hidden" to see the "Authorization" header
show hidden
see the auth header
I am calling basic auth rest api. seperate java program is working properly but when I add it into Grails application it throws exception saying
errors.GrailsExceptionResolver - SocketException occurred when processing request: [GET]
Message: Connection reset
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://....")
.get()
.addHeader("Authorization", "Basic Q...")
.addHeader("cache-control", "no-cache")
.addHeader("Postman-Token", "77768b44-678d-4803-8ea2-46767192b161")
.build();
Response response = client.newCall(request).execute();
I have another one for logging in. So this is how I see it, every time I try running this code, it asks me to authenticate.
So this is how it works I suppose, I basically have to run the login code first, store the cookies sent in response by the server and use it for subsequent operations, like for example making changes in the servers database.
I am able to make these changes via postman.
so how do i receive the cookies sent in response by the server?
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("multipart/form-data; boundary=----
WebKitFormBoundary7MA4YWxkTrZu0gW");
RequestBody body = RequestBody.create(mediaType, "------
WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
name=\"username\"\r\n\r\nadmin\r\n------
WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
name=\"password\"\r\n\r\nadmin\r\n------
WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
name=\"this_is_the_login_form\"\r\n\r\n1\r\n------
WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
name=\"post_data\"\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--");
Request request = new Request.Builder()
.url("http://localhost:8081/iclock/accounts/login/?
next=/iclock/data/iclock/")
.post(body)
.addHeader("content-type", "multipart/form-data; boundary=----
WebKitFormBoundary7MA4YWxkTrZu0gW")
.addHeader("Cache-Control", "no-cache")
.addHeader("Postman-Token", "86439550-93f7-6d60-1aab-7d289c137b0d")
.build();
Response response = client.newCall(request).execute();
response.headers("Set-Cookie") returns the cookie sent by the server in a String format. Refer this for more information related to the Response class in OkHttp.
Is there any way to Passing Multiple URL's for OKhttp url Request type. Actually i want pass multi url's like website, mobile request urls(Androd, mobile browser, tablet)
Request request = new Request.Builder()
.url("multi url" + ---
.addheader(---).build();
No
The OkHttp Request object allows one and only one URL.
If you require several requests, create different requests:
Request stackoverflowRequest = new Request.Builder()
.url("https://www.stackoverflow.com/")
.addHeader(...)
.build();
Request googleRequest= new Request.Builder()
.url("https://www.google.com/")
.addHeader(...)
.build();
If you don't want to write all the headers X times, you can use the following:
Request templateRequest = new Request.Builder()
.url("https://www.example.com/")
.addHeader(...)
.build();
Request stackoverflowRequest = templateRequest.newBuilder()
.url("https://www.stackoverflow.com/")
.build();
Request googleRequest = templateRequest.newBuilder()
.url("https://www.google.com/")
.build();