Please help with following problem.
When I try to do request to gitlab with Postman or curl everithing works fine, I got answer with the file
curl --header "PRIVATE-TOKEN: xxxxxxxx" "https://gitlabXXXX/api/v4/projects/13/repository/files/src%2Fcom%2Fgre%2Fjenkins%2FConstants.groovy?ref=foo"
But when I try to do the same in code I get error with this message = "404 File not found"
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("PRIVATE-TOKEN", "xxxxxxx");
System.out.println(restTemplate.exchange("https://gitlabXXXX/api/v4/projects/13/repository/files/src%2Fcom%2Fgre%2Fjenkins%2FConstants.groovy?ref=foo", HttpMethod.GET, new HttpEntity<>(httpHeaders), String.class).getBody());
Why it does not work? Maybe something in RestTempate change the URL or I do not know...
RestTemplate encodes your URL once again, so your request URL is:
https://gitlabXXXX/api/v4/projects/13/repository/files/src%252Fcom%252Fgre%252Fjenkins%252FConstants.groovy?ref=foo
So your % turns into %25 and this is not what gitlab API is waiting for.
Solution
You can use UriComponentsBuilder.build(true) method to tell your URI is already encoded:
String gitlabUriString = "https://gitlabXXXX/api/v4/projects/13/repository/files/src%2Fcom%2Fgre%2Fjenkins%2FConstants.groovy?ref=foo";
// true in build(true) tells parameters are already encoded
URI gitlabUri = UriComponentsBuilder.fromHttpUrl(gitlabUriString)
.build(true).toUri();
System.out.println(restTemplate.exchange(gitlabUri, HttpMethod.GET, new HttpEntity<>(httpHeaders), String.class).getBody());
Related
When I start my Spring server, I see this in the logs: Tomcat initialized with port(s): 9002 (http). So, I get the impression that it will by run locally on port 9002. If use my browser to go to this url http://localhost:9002/, then I get a 404. I'll also try to send a POST based on this endpoint in the spring code
#ApiOperation(value = "GET a List of Messages")
#GetMapping(value = "/msg", produces = MediaType.APPLICATION_JSON_VALUE)
#ResponseBody
public ResponseEntity getMessageList(HttpServletRequest request) {
List<MessageVo> result = msgService.getMessageList(request.getQueryString());
HttpHeaders responseHeaders = new HttpHeaders();
return new ResponseEntity<>(result, responseHeaders, HttpStatus.OK);
}
When I post to http://localhost:9002/msg, I also get a 404. Any work arounds why it's not displaying correctly?
You are missing context name in your URL.
http://localhost:9002/<context>/msg
Try posting request to the below URL :-
http://localhost:9002/msg
We are trying to access the Lithium Rest Api using HTTPS through Spring Rest Template as below
String plainCreds = lswUserName + ":" + lswPassword;
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
HttpEntity<String> request = new HttpEntity<String>(headers);
ResponseEntity<String> sRawResp = restTemplate.exchange(completeURL.toString(), HttpMethod.GET, request, String.class);
Above code always gives
org.springframework.web.client.HttpClientErrorException: 401 Unauthorized Exception
There were no issues when invoking the same web service through postman, however when ever accessed through java code, getting the above 401 unauthorized exception
Previously I faced this issue with Windows OS, and I added below entry related to proxy in to catalina.bat:
JAVA_OPTS=%JAVA_OPTS% -Dhttp.proxyHost=hy**1.*****.co.in -Dhttp.proxyPort=8080`
Now we moved the entire setup to Linux OS and I get the same error. In Linux I don't have any proxy set. I checked through wget http://www.google.com and no proxy details are displayed.
How do I resolve this issue in Linux when no proxy is set?
Please Help.
I'm trying to make a get request to an Api Rest but I'm always getting 404, nevertheless if try copying queryUrl in a browser or postMan it works perfectly.
restTemplate.getForObject(queryUrl, entity ,Integer.class);
I've also tried this:
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity entity = new HttpEntity(httpHeaders);
log.debug("request headers: " + entity.getHeaders());
ResponseEntity response = restTemplate.exchange(queryUrl,
HttpMethod.GET, entity, String.class);
But nothing changes.
Can anyone help me?
This took me a bit to figure out, so I'll leave a little note here hoping one day it helps someone.
I kept getting a 404 when making a get request with ResTemplate.getForEntity(URL, Type). The URL worked in Chrome, Postman, but not in my code.
It ended up being because I had an unsafe character in my URL, an % to be exact, because my URL had spaces and so instead of space the URL had %20. Postman and Chrome could handle this, but not my code. Once I replaced the unsafe character with an actual space all was well.
Hope it works out for you too.
You were right Barath the usage is correct. The problem was that UriComponentBuilder was including a blank space at the en of the URL.
I've fixed it with a trim.
String queryUrl = UriComponentsBuilder.fromUriString(HOST)
.path(PATH)
.buildAndExpand(param)
.toUriString().trim();
When your url contains "%", then you shoud use restTemplate.getForObject(new URI(url),xx.class), it worked for me.
I have the following scenario. I have an XML file:
query-users.xml
<?xml version="1.0"?>
<q:query xmlns:q="http://prism.evolveum.com/xml/ns/public/query-3">
</q:query>
When executing the curl commend:
curl.exe --user administrator:5ecr3t -H "Content-Type: application/xml" -X POST http://localhost:8080/midpoint/ws/rest/users/search -d #C:\Users\user\query-users.xml
I get the desired response in XML.
I am trying to do the same POST request using RestTemplate from JAVA code:
try{
StringBuilder builder = new StringBuilder();
builder.append("http://localhost:8080/midpoint/ws/rest/users/search");
builder.append("?query=");
builder.append(URLEncoder.encode("<?xml version=\"1.0\"?><q:query xmlns:q=\"http://prism.evolveum.com/xml/ns/public/query-3\"></q:query>"));
URI uri = URI.create(builder.toString());
restOperations.postForEntity(uri, new HttpEntity<String>(createHeaders("username", "pass")), String.class);
logger.info(response);
}catch(Exception e){
logger.info(e.getMessage());
}
}
I get Internal Servel Error .
There is something that I am doing wrong passing the XML string to the POST request with RestTemplate, but I am not figuring out what it is.
Is there a way how to solve this?
Thanks
Your curl invocation and RestTemplate call are not equivalent. In first one you pass your xml as a a body of HTTP Request (this is what -d option does). In your RestTemplate you assign your xml to query and consequently HTTP Request has no payload and your data is encoded in URL.
If you want to pass your xml as a HTTP Body, you should use different HttpEntity constuctor: http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/HttpEntity.html#HttpEntity-T-org.springframework.util.MultiValueMap-
I am trying to fetch all job names by using below code
HttpGet httpGet = new HttpGet("http://myjenkins/api/json?depth=1&tree=jobs[name,jobs[name]]")
try(CloseableHttpClient httpclient = HttpClients.createDefault()) {
try(CloseableHttpResponse response = httpclient.execute(httpGet)){
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
String json = EntityUtils.toString(entity);
System.out.println(json)
}
}
}
The above code is not returning any json response (just an empty array []), but if I remove tree query in the url (http://myjenkins/api/json?depth=1), then I get json response with all jobs.
Why the query with filter is not returning any results ?. Is something wrong with HttpClient or jenkins api.
Can someone help me to resolve this issue.
Thanks
I would suggest just trying out that url in a browser. I tried it against my instance of jenkins, and it worked fine.
Also, the second parameter in the tree query seems unnecessary - even this url returns the job names -
http://myjenkins/api/json?depth=1&tree=jobs[name]
Couple of things ..
1) I tried it my browser and all queries worked well but not through java code. The reason is, in browser am already signed in (git oauth) and all queries are working where as in java am getting empty array since jenkins authorization set to not read jobs for anonymus (Stupid of me not check this before).
2) Once proper permissions are set I still had an issue with URI encoding, then I used URI builder
URI uri = new URIBuilder().setScheme("http").setHost(jenkinsHost)
.setPath("/api/json")
.setParameter("depth", "1").setParameter("tree", "jobs[name,jobs[name]]")
.build()
everything works now.