I have Azure logic app with request trigger. I want to trigger this logic app from my java application. So, I am trying to call the request trigger url from my java API.
It is working fine if i am hitting in postman, logic app with request trigger hitting via postman
but getting 401 on calling it using RestTemplate in java.
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
@RestController
public class SampleController {
@RequestMapping(value="/display",method = RequestMethod.GET)
public ResponseEntity<String> display()
{
System.out.println("entered");
RestTemplate restTemplate = new RestTemplate();
String url = "https://pt-38.northeurope.logic.azure.com/workflows/cc0363a3bf134efca43b0bd2d15d5ed5/triggers/manual/paths/invoke?api-version=2016-10-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=HnllGcN9xo91kRGAjk";
HttpHeaders headers = new HttpHeaders();
headers.set("api-version", "2016-10-01");
headers.set("sp", "%2Ftriggers%2Fmanual%2Frun");
headers.set("sv", "1.0");
headers.set("sig", "HnllGcN9xo91kRGAjk3Zlp6fW0dhwf");
HttpEntity request = new HttpEntity(headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
// JSONObject obj = new JSONObject(response.getBody());
// System.out.println("obj");
return response;
}
}
logic app with request trigger accessing via rest template
As per Hury Shen suggestion i changed api code as below,
public HttpEntity<String> display1()
{
System.out.println("entered");
RestTemplate restTemplate = new RestTemplate();
String url = "https://prod-38.northeurope.logic.azure.com/workflows/cc0363a3bf134efca43b0bd2d15d5ed5/triggers/manual/paths/invoke?api-version=2016-10-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=HnllGcN9xo91kRGAjk3Zlp6fW0dhwfaRij-fk1CC60c";
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
.queryParam("api-version", "2016-10-01")
.queryParam("sp", "%2Ftriggers%2Fmanual%2Frun")
.queryParam("sv", "1.0")
.queryParam("sig", "HnllGcN9xo91kRGAjk3Zlp6fW0dhwfaRij-fk1CC60c");
HttpEntity<?> entity = new HttpEntity<>(headers);
HttpEntity<String> response = restTemplate.exchange(
builder.toUriString(),
HttpMethod.GET,
entity,
String.class);
return response;
}
For this problem, we should put the value of api-version
, sp
, sv
and sig
in query parameters instead of put them in headers.
Do it like this:
Map<String, String> params = new HashMap<String, String>();
params.put("api-version", "xxxx");
params.put("sp", "xxxxx");
params.put("sv", "xxx");
params.put("sig", "xxxxxxx");
.....
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class, params);