I have been struggling to simply perform a GET request to the Spotify API and parse the results of the request. I haven't been able to successfully do it despite trying to follow the docs. I am trying to achieve this within an Actor. I have tried making the HttpEntity a strict HttpEntity but this returns an error. I am not sure if this is due to dependency issues or if it is simply the incorrect approach. What I would like to do is obtain the entire payload of the response and parse it to then return it to another actor. I am unsure what should go inside of the try block to achieve this. Thanks very much.
ActorSystem actorSystem = getContext().getSystem();
Http http = Http.get(actorSystem);
String endpoint = "https://api.spotify.com/v1/artists/1moxjboGR7GNWYIMWsRjgG/top-tracks?market=IE";
Authorization authorization = Authorization.oauth2("REDACTED");
HttpRequest request = HttpRequest.create().withUri(endpoint).addHeader(authorization);
CompletionStage<HttpResponse> responseFuture = http.singleRequest(request)
responseFuture.thenAccept(response -> {
try {
}
Have tried casting entity to a strict entity but this results in an error.
Below is the Python code of what I want to do specifically. I want to implement the same functionality in JAVA without using any third party packages. I will be making the POST request and will receive a JSON response. I am not interested in anything else, it should just send the post request to the specified url and get the response and display/store it.
import requests
url = "http://localhost/imageOpener.py"
files = open('green_jay.jpeg', 'rb')
r = requests.post(url, data=files)
print r
print r.content
When I send a request using a proxy client, if I get a certain response, I would like to be able to modify the request and then send the same request again for all requests.
Normally I would do something like:
BookStore proxy = JAXRSClientFactory.create("http://books", BookStore.class);
try
{
proxy.getBook("someId");
}
catch(WebApplicationException ex)
{
Response r = ex.getResponse();
if (r.getStatusCode() == 404)
{
proxy.getBook("anotherId");
}
}
But in this case, there is a common thing I want to do for all requests: If I get a specific http code, modify some header values, and then try again (probably with a limit on the amount of retries).
I haven't seen a way that cxf proxy clients explicitly support this, how could I go about implementing it?
You need to write an interceptor to do this for every request.
here you go for sample code and documentation http://cxf.apache.org/docs/jax-rs-filters.html
I'm new to playframework and to REST API.
I want to send a POST request to REST API in a controller.
What is the best way to do it? Does play have a support for it or do I have to use a plugin?
Basically I want it to look like this:
User submits a form.
I load a form data in a controller.
I send form data as a POST request
Get response, do something with it and display result
So far I'm stuck at point 3, I have no idea how to do this.
Code to visualize what I have in mind:
public static Result processForm() {
Form<FormData> myForm = Form.form(FormData.class).bindFromRequest();
String text = myForm.get().text;
//Send 'text' in a post request and get response
text = doSomethingWithResponse(response);
return ok(resultpage.render(text));
}
I don't think it matters but this is the API I want to use:
http://open.xerox.com/Services/fst-nlp-tools/Pages/API%20Docs
The following line of code sends a request and waits for a reponse:
WS.url(feedUrl).setHeader("Content-Type", "application/x-www-form-urlencoded").post("arg1=val1&arg2=val2").get().asJson();
I am developing web services using Restlet Java.
For this I want to protect some webservices from unauthorized clients. So I have written Filter class. In that Filter class I want to get the headers of the Request. But I am getting the following error -
java.lang.ClassCastException: org.restlet.engine.http.HttpRequest cannot be cast to javax.servlet.http.HttpServletRequest
The coding is -
public class MyFilter extends Filter {
#Override
protected int beforeHandle(Request request, Response response) {
int result = STOP;
HttpServletRequest httpReq = (HttpServletRequest) request;
String user_token = httpReq.getHeader("auth");
if(user_token.equals("xyz")) {
result = CONTINUE;
}
return result;
}
}
Please suggest me a way to access the header values of Request in Java Restlet?
I solved my problem using
Form headers = (Form) request.getAttributes().get("org.restlet.http.headers");
String user_token = headers.getFirstValue("Location");
I found this http://blog.yudongli.com/2009/12/get-request-header-in-restlet_13.html link useful.
Please also notice that Restlet provides an API for RESTful application. This means that you can access standard headers using this API. In most cases, you donn't need to use the attribute named "org.restlet.http.headers".
For example, if you want to set a Location header in the response, you add this code:
getResponse().setLocationRef("http://...");
Otherwise, since you talk about security, Restlet provides a generic API to support such aspect (see classes ChallengeAuthenticator, Verifier, Enroler).
Hope it helps you.
Thierry
If you are using Restlet 2.1-RC3 this is the way to get it
Series<Header> headers = (Series<Header>) getRequestAttributes().get("org.restlet.http.headers");
String auth = headers.getFirstValue("auth");
This is exactly how it's worked for me. None of the answers above did it. I hope it helps.
How would you handle seeking to parse an ID in the GET header from a client request ?
Would you approach this in the same manner ?
Since the:
Form headers = (Form) getRequestAttributes().get("org.restlet.http.headers");
returns all the header information and
String hID = headers.getFirstValue("Location");
gets the location(s)
How would you handle parsing out the ID ?
Series headers = (Series) getRequestAttributes().get("org.restlet.http.headers");
String origin = headers.getFirstValue("Origin");`
This is just an example of getting the Origin header. If you want to get Location, just change to headers.getFirstValue("Location");
As in the new version of Restlet, getRequestAttributes().get() returns Series instead of Form.