-1

I call my https REST APi that calls http api on another server using Postman, and it returns 502 bad gateway error. Authentication for https API is set in a header and accept:application/json as well. All other https rest apis that not calling ohter http apis on remote server works fine.

I've created REST APIs with authentication using SpringBoot. There are GET and POST methods. They all have URL like https://.... Before calling them, user has to get a security token. Those Rest apis are available as war file residing on Server_1 deployed in tomcat. From a number of the APIs, I call another Rest services residing as jar file on another Server_2 and its URL has a URL like http://.... When I call one https rest api residing in war file on Server_1, the request goes through to Server_2. Http rest api on Server_2 answers with the JSON file. I can see it in the logs of Server_2, plus I've captured the http traffic between servers. It looks like http rest api receives the request and sends the answer back, but then https RestApi from Server_1 rejects the answer because it's not encrypted and gives bad gateway 502 error. If there is any way to fix this error?

//This is how I call http rest api on server 2. This code is part of https rest api on Server 1(war file in tomcat)

public ResponseEntity<String> function()
{
RestTemplate restTemplate = new RestTemplate();
String myUrl = "http://xxxxxx:8081";
ResponseEntity<String> response = restTemplate.getForEntity(myUrl + "/api/test/suspend/12", String.class);
return responce
}
Konstantin P.
  • 11
  • 1
  • 1
  • 5

2 Answers2

0

You got a few small issues in your code The last line (return statement) needs to finish with a semicolon and you wrote „responce“ instead of „response“.

public ResponseEntity<String> function()
{
  RestTemplate restTemplate = new RestTemplate();
  String myUrl = "http://xxxxxx:8081";
  ResponseEntity<String> response = restTemplate.getForEntity(myUrl + "/api/test/suspend/12", String.class);
  return response;
}

You should additionally use the content of the response of the HTTP request and use it as the content of your actual HTTPS request, otherwise you will mix the requests which will not work.

Daniel Fuchs
  • 36
  • 1
  • 5
  • Hi Daniel, thank you for your corrections. My problem is that call to https rest api which calls http rest api doesn't return anything, no matter that i can see http rest api returns the value. Do you think that "content" thing will help? I return the responce to controller and it returns the responce of the type ResponseEntity directly to user. So I think i do return the content of http strait away to https. But still nothing. – Konstantin P. Aug 22 '19 at 08:09
  • When I've checked the response of the http - the data was of the type "chunked data". Could it be the case that I wait for the json and that's the reason the output marked as 502 error? So could it be fixed probably with additional headers somehow? – Konstantin P. Aug 22 '19 at 08:09
  • Sorry for the long time since my last reply, was on vacation. My guess is that you should wait for all data chunks, then parse them as a String and after that send the answer back with your HTTPS request. – Daniel Fuchs Sep 02 '19 at 13:26
  • Also this answer here could help you, just give it a try: https://stackoverflow.com/a/30960274/4763898 – Daniel Fuchs Sep 02 '19 at 13:39
0

In postman, under Authorization tab, there is Type listbox, please select "No Auth"

Christopher
  • 425
  • 3
  • 9
  • Christopher, thank you for your answer. If I won't use authorisation in postman then my https rest api which calls http rest api won't be available because of no security token. – Konstantin P. Aug 22 '19 at 08:01