Parameters for request HttpGet aren't set - java

I try to set parameters for request Get by
HttpGet.setParams method (new BasicHttpParams () .setParameter ("oauth_token", "Valid_token"))
to receive a request similar brought below:
https://api.soundcloud.com/me?oauth_token=Valid_token
But when sending request by HttpClient.execute (request) method;
In logs shows that the oauth_token parameter wasn't added to request, prompt, in what a problem. When I inscribe the correct line in the browser everything works, in this case parameter simply isn't added to request

I found a solution, it was necessary to set parameters not through the setParams method (), and obviously to set them in URI, how here https://api.soundcloud.com/me? me? oauth_token=A_VALID_TOKEN

Related

I am getting 400 Bad Request for ReST call only in internet explorer

In our running application, one of GET request starts giving response as 400 Bad Request in Internet Explorer.
On investigating , I found that GET request doesn't have queryParameters what were expected by ReST call.
As it is giving response in another browsers like Chrome, Mozilla,
how can I proceed further ?
this is Request currently being triggered--
Method of request is GET
https://XXXXXXXXX/XXX/XXXXXXXXX/XXXXXXXXX/XXXXXXXXX/XXXXXXXXX?{%22numRecords%22:1000,%22start%22:0}&_=1487576597960
and queryParameters in #QueryParam expected by ReST api are-
-numRecords
-start
I know by the above GET request, numRecords and start will not get captured by api backend.
So , is there any chance, if my GET request lack of any #QueryParam will lead to 400 Bad request response.
I found that GET request doesn't have queryParameters
You can use query parameters in GET requests as you've provided in your example like this: http://host?queryParam1=value1. however it's not possible to pass a request body as you can do for POST or PUT requests to provide JSON encoded data for example. You can work around this by adding the JSON AND URL encoded payload to a query parameter. But you endpoint explicitely has to be able to read this parameter. So you should add this parameter to your definiotn like in this example for JAX-RS:
#GET
#Path("my-endpoint")
public String request(
#PathParam("payload") JsonObject payload
) {
What you've tried is to simply pass the payload data without specifing the name of the request parameter.
Hope this helps.

need to pass some content in header in get request of JSON using restTemplate

I need to consume the rest service using my rest client.
From the 1st URL, need to fetch some content (in this case it is a JSON web token).
And in the 2nd URL, need to pass this token in request header. Please help me achieving the same.
Just to note, the 1st URL is with POST and whereas the second URL is with GET request.
With the help of RestTemplate api, I am able to get the response body and response header when passing the first URL in method PostForObject(url, map, String.class). From Response header, I am able to get the details about pragma, cache, content-type, server, content-length, expires etc but not able to fetch the JWT string (Json Web Token). I need to get the JWT as I need to pass this into the request header of 2nd URL (GET) to get the actual information.
You can make use of Path Params to fetch data from the URL and use the same to add to a different URL-
Example:
#Path("/{taskId}")
public Task get(#PathParam("taskId") Long taskId)
{
// Handles GET on /tasks/{taskId}. Returns a single task for the given taskId.
return taskService.find(taskId);
}
for more details visit : http://java.dzone.com/articles/spring-3-webmvc-optional-path
If this helps you, mark as answer
I could get this info, I am posting the same as below in case someone needs the same.
Collection<List<String>> headerContent = restTemplate.postForEntity(URI url,
Object request, Class<T> responseType),getHeaders().values();

HttpClient parameters not being added to the executing method

Sorry that it's potentially an easy answer but I can't find anything.
I currently have the method:
public MediaSource getConvertedMediaServletCall(String format) throws HttpException, IOException{
HttpClient httpclient = new HttpClient();
GetMethod httpGet = new GetMethod(MEDIA_SERVER_URL);
httpGet.getParams().setParameter("format", format);
httpGet.getParams().setParameter("handler", "handle");
try{
int statusCode = httpclient.executeMethod(httpGet);
byte[] responseBody = httpGet.getResponseBody();
Now I know this doing really do anything, this has to be worked on once I get the problem working. The problem is, I am create what I believe a http client, which executes the get method. Now the problem is when the code runs the httpClient.executeMethod(httpGet); the servlet doGet method is being executed, and I know this for a fact, but the parameters are never passed through from this executing method.
Anyhelpwould be appreciated..
For anyone still wondering, although HttpClient gives you the methods for adding parameters, it won't work. Get parameters have to passed in the URL, and these parameters are being added to the body of the message, like a post request.
The workaround to this is discussed here: How do I add query parameters to a GetMethod (using Java commons-httpclient)?

How can I POST using Java and include parameters and a raw request body?

I am communicating with a web service that expects a POST parameter and also expect Request body. I have confirmed that such a POST request can be done using a REST Console I have, but I am unable to make such a request in Java using Apache libraries.
In the code below, I am able to POST to the web service, and it correctly receives the contents of the variable raw_body. If I uncomment the first of the two commented lines, the web service receives the "fname" parameter, but it no longer receives the body of the POST.
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
...
HttpClient httpClient = new HttpClient();
String urlStr = "http://localhost:8080/MyRestWebService/save";
PostMethod method = new PostMethod(urlStr);
String raw_body = "This is a very long string, much too long to be just another parameter";
RequestEntity re = new StringRequestEntity(raw_body, "text/xml", "UTF-16");
//method.addParameter("fname", "test.txt");
//httpClient.getParams().setParameter("fname", "test.txt");
method.setRequestEntity(re);
How can I transmit both the parameter and the body?
You could use the setQueryString method to add the parameters to the URL that is being POSTed to. From a RESTful perspective I'd argue you should normally not be doing that, however, since a POST should represent a call to a resource and anything that would qualify for a query parameter should be included in the representation that is being transferred in the request body...or it should represent qualification of the resource itself in which case it should be part of the path that is posted to which could then be extracted by the controller using #PathVariable/#PathParam or something similar. So in your case you could also be looking for something like POST /MyRestWebService/files/test.txt or more fittingly a PUT if you're saving the resource and know the URI. The code on the server could pull the filename out from a URL pattern.
You need to make a POST request using multipart-form. Here is the example:
Apache HttpClient making multipart form post
Alternatively, you can make a POST request with the content (parameters and files) encoded using application/x-www-form-urlencoded but it is not recommended when you want to make a POST request with large content, like files.

jmeter not able to accept parameters?

I have a weird problem.
I am not able to send request parameters to the LocalHost while uploading an image.
After selecting HTTP request sampler, I add request parameters, the add a file and parameter name for it. If I don't put parameter name for the image it accepts the request parameters, I put the parameter name for the image, then it doesn't accept the request parameters.
What could be the problem?
PS: I have HTTP cookie manager, HTTP request(for logging in and get the session), then another HTTP request for sending request parameters and image with the parameter name. At last View Results Tree.
The easiest way to format your request is to RECORD the action, and then modify the parameters. This guarantees several things:
You have the correct method (POST / GET / etc)
All parameter names are correct (the wrong character case can kill you)
All parameters are captured.
Additionally, JMETER has as field at the bottom of the HTTP request for attaching additional files. It is here that you need to specify the full file path and file type. This will most likely mirror what you've put into the parameters.

Categories

Resources