How to call a Jira cloud post rest call using Resttemplate in Spring Boot addon

Hi
I am trying Jira Post rest calls Using Spring Boot-rest template . But for every rest call I am getting 400 error .That means I think I am Passing wrong data. In Postman it is working.But the problem is while calling through Java code it is adding one so that data it is not taking.
I tried like this.
Any Suggestion how to try Jira Cloud post rest calls in Plugin using resttemplate or uNirest .

HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		String username = "chakresh@acis.io";
		String password = "api-token";
		String auth = username + ":" + password;
		byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
		String authHeader = "Basic " + new String(encodedAuth);

		headers.set("Authorization", authHeader);
		// correct data
//		{
//			"options":[{"value":"Asset Type"}]
//		}

		JSONObject jo = new JSONObject();
		jo.put("value", name);

		JSONArray ja = new JSONArray();
		ja.put(jo);

		JSONObject mainObj = new JSONObject();
		mainObj.put("options", ja);

		System.out.println("mainObj====================" + mainObj);
		String json = null;

		json = mainObj.toString();
		System.out.println("json==================" + json);

		AtlassianHost host = hostUser.getHost();
		HttpEntity<String> entity = new HttpEntity<String>(json, headers);
		System.out.println("entity============" + entity);
		UriComponentsBuilder builder = UriComponentsBuilder
				.fromHttpUrl(host.getBaseUrl() + "/rest/api/3/field/" + customfieldidnew + "/option");
		RestTemplate restTemplate = atlassianHostRestClients.authenticatedAsAddon();
		ResponseEntity<String> response = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.POST,
				entity, String.class);```

Hi @ChakreshTiwari,

I assume you are using the Atlassian Connect Spring Boot framework? If yes, have you read its readme? There you should find the answer to your question:

Making API requests to the product as the add-on

atlassian-connect-spring-boot will automatically sign requests from your add-on to an installed host product with JSON Web Tokens. To make a request, just autowire an AtlassianHostRestClients object into your class.

When responding to an incoming request, outgoing requests to relative URL’s will be made to the current authenticated host product.

@Autowired
private AtlassianHostRestClients atlassianHostRestClients;

public void doSomething() {
    atlassianHostRestClients.authenticatedAsAddon().getForObject("/rest/api/example", Void.class);

Just inject yourself the AtlassianHostRestClients instance and get yourself a RestTemplate that will authenticate itself correctly that way. :slight_smile:

Cheers,
Sven

P.S. You might also be interested in the official connect spring boot sample projects - they’re super helpful.

@ChakreshTiwari have a look at this post for bumping the log level for RestTemplate, and you should hopefully see a detailed 400 error message in your application log.

1 Like

Hi @sven.schatter
Thanks for your response , I am already using this

@Autowired
private AtlassianHostRestClients atlassianHostRestClients;

Here is my code inside method with header and json data

	JSONObject jo = new JSONObject();
		jo.put("value", name);

		JSONArray ja = new JSONArray();
		ja.put(jo);

		JSONObject mainObj = new JSONObject();
		mainObj.put("options", ja);

		System.out.println("mainObj====================" + mainObj);
		String json = null;

		json = mainObj.toString();
		System.out.println("json==================" + json);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		AtlassianHost host = hostUser.getHost();
		HttpEntity<String> entity = new HttpEntity<String>(json, headers);
		System.out.println("entity============" + entity);
		UriComponentsBuilder builder = UriComponentsBuilder
				.fromHttpUrl(host.getBaseUrl() + "/rest/api/3/field/" + customfieldidnew + "/option");
		RestTemplate restTemplate = atlassianHostRestClients.authenticatedAsAddon();
		ResponseEntity<String> response = restTemplate.postForEntity(builder.toUriString(), entity, String.class);
		atlassianHostRestClients.authenticatedAsAddon().getForObject(builder.toUriString(), String.class);
		String responsetemp = response.getBody();
		System.out.println("responsetemp===================" + responsetemp);

In terminal after calling rest call look what is coming – in screenshot.

Please provide some suggestion for this how to fix this error.
Thanks.

Hi @epehrson
I tried adding this in yml file

logging.level.org.apache.http: DEBUG.

and this bean in main File

 @Bean
	public RestTemplateCustomizer httpComponentsRestTemplateRequestFactoryCustomizer() {
	    return restTemplate -> restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
	}

But still getting same error . I have added screenshot also Please have a look and give some help to solve this.

Have you tried doing a simple call without all of your extra headers and so on? Something like:

String json = atlassianHostRestClients.authenticatedAsAddon(host).getForObject("/rest/api/3/serverInfo", String.class);

Hi @sven.schatter

I tried this code

AtlassianHost host = hostUser.getHost();
		String json = atlassianHostRestClients.authenticatedAsAddon(host).getForObject("/rest/api/3/serverInfo", String.class);
		return ResponseEntity.ok(json);

It is working fine.
But then same way I tried for above post rest call

		String responsetemp=	atlassianHostRestClients.authenticatedAsAddon(host).postForObject("/rest/api/3/field/" + customfieldidnew + "/option",entity, String.class);

But then again the same error.
I think while writing time it is adding one extra square bracket around data that was mistake or any suggestion from Your side.
Thanks

Sounds like the URL you’re building (or your POST body) might be invalid then. Have you tried URL encoding customfieldidnew?

@sven.schatter Thanks for response
I think Url is correct i tried builder.toUriString() , This customfieldidnew is initially string Like this customfield_10137 then replace and get 10137 and then converted that to integer then passed.

I think URL is correct it is coming like this-
/rest/api/3/field/10137/option
and body that is like this
Writing [{“options”:[{“value”:“Asset Type”}]}] as “application/json”
after this 400 error.
Thanks

Hi @sven.schatter and @epehrson
Now it worked actually i am passing data by forming json when i passed as java Object then it worked
Thanks for your sugeestions.

1 Like