# Microservice SDK (java) best way to call unsupperted but available REST endpoints

**URL:** <https://community.cumulocity.com/t/microservice-sdk-java-best-way-to-call-unsupperted-but-available-rest-endpoints/7747>\
**Category:** Forum\
**Tags:** cumulocity\
**Created:** [March 2, 2023, 8:04am UTC](https://community.cumulocity.com/t/microservice-sdk-java-best-way-to-call-unsupperted-but-available-rest-endpoints/7747 "2023-03-02T08:04:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Pester](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/alexander_pester/32/5725_2.png) [@Alexander\_Pester](https://community.cumulocity.com/u/Alexander_Pester)\
**Post date:** [March 2, 2023, 8:04am UTC](https://community.cumulocity.com/t/microservice-sdk-java-best-way-to-call-unsupperted-but-available-rest-endpoints/7747/1 "2023-03-02T08:04:21Z")

</div>

#### What product/components do you use and which version/fix level are you on?

Java Microservice SDK 1016.0.117

#### What are you trying to achieve? Please describe it in detail.

I want to use functionality to upload and download binaries to events with my Microservice, see [Cumulocity - OpenAPI](https://cumulocity.com/api/core/#tag/Attachments)

I suppose i have to inject com.cumulocity.sdk.client.RestConnector; and call methods:

//upload  
restConnector.postFile(url, null, attachment, EventRepresentation.class);

//download  
restConnector.get(url, MediaType.APPLICATION\_OCTET\_STREAM\_TYPE);

My Questions:

1. Where is the best way to get the different parts of the url?

```auto
??platformUrl??+"/event/events/{{eventId}}/binaries";

```

If this method would be public: [cumulocity-clients-java/java-client/src/main/java/com/cumulocity/sdk/client/event/EventApiImpl.java at 3b199d143918dea1d7c21f24c4031db5b4e0940a · Cumulocity-IoT/cumulocity-clients-java · GitHub](https://github.com/Cumulocity-IoT/cumulocity-clients-java/blob/3b199d143918dea1d7c21f24c4031db5b4e0940a/java-client/src/main/java/com/cumulocity/sdk/client/event/EventApiImpl.java#L102)  
I had no problem to consturct the URL for it. I could directly use EventApiImpl. However it is private, how could i construct that with new version?

1. What is the second parameter of method postFile i have set to null. It must be from type EventRepresentation, i don’t have a request body for uploading the file? I don’t understand how to initialize this parameter.

---

<div class="post-metadata">

**Author:** ![Korbinian\_Butz](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/korbinian_butz/32/10473_2.png) [@Korbinian\_Butz](https://community.cumulocity.com/u/Korbinian_Butz)\
**Post date:** [March 2, 2023, 8:22am UTC](https://community.cumulocity.com/t/microservice-sdk-java-best-way-to-call-unsupperted-but-available-rest-endpoints/7747/2 "2023-03-02T08:22:48Z")

</div>

Can only comment on your first question…

> Where is the best way to get the different parts of the url?

The platform URL should be available as environment variable C8Y\_BASEURL.

See [here](https://cumulocity.com/guides/microservice-sdk/concept/#authentication-and-authorization): “[…]The Cumulocity IoT exposes the REST endpoint /user/currentUser. The microservice retrieves the Cumulocity IoT address from the `C8Y_BASEURL` operating system environment variable.”

---

<div class="post-metadata">

**Author:** ![Harald\_Meyer](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/harald_meyer/32/1959_2.png) [@Harald\_Meyer](https://community.cumulocity.com/u/Harald_Meyer)\
**Post date:** [March 2, 2023, 8:36am UTC](https://community.cumulocity.com/t/microservice-sdk-java-best-way-to-call-unsupperted-but-available-rest-endpoints/7747/3 "2023-03-02T08:36:48Z")

</div>

Be aware that C8Y\_BASEURL will not work correctly in a multi-tenant microservice. In that case you can extract the platform url from the response to the /tenant/currentTenant endpoint.

Update: I might be wrong, will test and follow up.

---

<div class="post-metadata">

**Author:** ![Stefan\_Witschel](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/stefan_witschel/32/31955_2.png) [@Stefan\_Witschel](https://community.cumulocity.com/u/Stefan_Witschel)\
**Post date:** [March 2, 2023, 9:23am UTC](https://community.cumulocity.com/t/microservice-sdk-java-best-way-to-call-unsupperted-but-available-rest-endpoints/7747/4 "2023-03-02T09:23:25Z")

</div>

> [@Alexander\_Pester](#):
>
> What is the second parameter of method postFile i have set to null. It must be from type EventRepresentation, i don’t have a request body for uploading the file? I don’t understand how to initialize this parameter.

It seems to be the object:  
[https://cumulocity.com/api/core/10.15.0/#operation/postEventBinaryResource](https://cumulocity.com/api/core/10.15.0/#operation/postEventBinaryResource)  
and must contain something like this

```auto
{ "name": "hello.txt", "type": "text/plain" }

```

see here: [cumulocity-clients-java/java-client/src/main/java/com/cumulocity/sdk/client/RestConnector.java at 3b199d143918dea1d7c21f24c4031db5b4e0940a · Cumulocity-IoT/cumulocity-clients-java · GitHub](https://github.com/Cumulocity-IoT/cumulocity-clients-java/blob/3b199d143918dea1d7c21f24c4031db5b4e0940a/java-client/src/main/java/com/cumulocity/sdk/client/RestConnector.java#L209)  
Not sure why this has to be something like a Representation.

---

<div class="post-metadata">

**Author:** ![Alexander\_Pester](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/alexander_pester/32/5725_2.png) [@Alexander\_Pester](https://community.cumulocity.com/u/Alexander_Pester)\
**Post date:** [March 2, 2023, 10:04am UTC](https://community.cumulocity.com/t/microservice-sdk-java-best-way-to-call-unsupperted-but-available-rest-endpoints/7747/5 "2023-03-02T10:04:47Z")

</div>

With new SDK and not allowing directly PlatformImpl and PlatformParameter, as i understand the output we don’t need that anymore:

Output:  
Use relative URL’s starting with ‘/’ with RestConnector bean to have them resolved automatically against the host defined in PlatformParameters#getHost().

Which sounds good! I don’t need to care about the absolute url anymore.

---

<div class="post-metadata">

**Author:** ![Michael\_Voigt2](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/michael_voigt2/32/18431_2.png) [@Michael\_Voigt2](https://community.cumulocity.com/u/Michael_Voigt2)\
**Post date:** [March 3, 2023, 2:35pm UTC](https://community.cumulocity.com/t/microservice-sdk-java-best-way-to-call-unsupperted-but-available-rest-endpoints/7747/6 "2023-03-03T14:35:29Z")

</div>

Just to add a little note here: I played around with the RestConnector and even if you provide a full URL (incl the host like “[https://something.com/](https://something.com/)…”) it will always connect to the host defined in PlatformParameters#getHost(): [cumulocity-clients-java/java-client/src/main/java/com/cumulocity/sdk/client/CumulocityHttpClient.java at f188384ef58ba2a743226e1d338862fb9981b49a · Cumulocity-IoT/cumulocity-clients-java · GitHub](https://github.com/Cumulocity-IoT/cumulocity-clients-java/blob/f188384ef58ba2a743226e1d338862fb9981b49a/java-client/src/main/java/com/cumulocity/sdk/client/CumulocityHttpClient.java#L62)

The reason I looked into this is: I want to change the behavior of the RestConnector for some calls when running in my local development environment (Eclipse). Those calls that target a REST endpoint within my own microservice should execute against [http://localhost:80/](http://localhost:80/)… so I can debug them accordingly. Anybody ever achieved this?

---

<div class="post-metadata">

**Author:** ![Alexander\_Pester](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/alexander_pester/32/5725_2.png) [@Alexander\_Pester](https://community.cumulocity.com/u/Alexander_Pester)\
**Post date:** [March 9, 2023, 11:56am UTC](https://community.cumulocity.com/t/microservice-sdk-java-best-way-to-call-unsupperted-but-available-rest-endpoints/7747/7 "2023-03-09T11:56:11Z")

</div>

I have implemented it using restTemplates:

Created two pojos EventBinary and BinaryInfo:

```java
@Data
public class BinaryInfo {
	/**
	 * Name of the binary object.
	 */
	private String name;

	/**
	 * Media type of the file.
	 */
	private String type;
}

```

```java
@Data
public class EventBinary {
	/**
	 * Name of the attachment. If it is not provided in the request, it will be set as the event ID.
	 */
	private String name;

	/**
	 * A URL linking to this resource.
	 */
	private String self;

	/**
	 * Unique identifier of the event.
	 */
	private String source;

	/**
	 * Media type of the attachment.
	 */
	private String type;
}

```

And Api:

```java
@Slf4j
@Service
public class EventAttachmentApi {
		
	private ContextService<MicroserviceCredentials> contextService;
	
	private EventApi eventApi;
	
	@Autowired
	public EventAttachmentApi(ContextService<MicroserviceCredentials> contextService, EventApi eventApi) {
		super();
		this.contextService = contextService;
		this.eventApi = eventApi;
	}
	

	public EventBinary uploadEventAttachment(final BinaryInfo binaryInfo, Resource resource, final String eventId) {
		EventRepresentation event = eventApi.getEvent(GId.asGId(eventId));
		if(event == null) {
			return null;
		}
		
		HttpHeaders headers = new HttpHeaders();
		headers.set("Authorization", contextService.getContext().toCumulocityCredentials().getAuthenticationString());
		headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
		headers.setContentType(MediaType.MULTIPART_FORM_DATA);
		
		MultipartBodyBuilder multipartBodyBuilder = new MultipartBodyBuilder();
		multipartBodyBuilder.part("object", binaryInfo, MediaType.APPLICATION_JSON);
		multipartBodyBuilder.part("file", resource, MediaType.TEXT_PLAIN);
		
		MultiValueMap<String,HttpEntity<?>> body = multipartBodyBuilder.build();
		HttpEntity<MultiValueMap<String, HttpEntity<?>>> requestEntity = new HttpEntity<>(body, headers);

		String serverUrl = event.getSelf() + "/binaries";
		RestTemplate restTemplate = new RestTemplate();
		ResponseEntity<EventBinary> response = restTemplate.postForEntity(serverUrl, requestEntity, EventBinary.class);
		if(response.getStatusCodeValue() >= 300) {
			log.error("Upload event binary failed with http code {}", response.getStatusCode().toString());;
			return null;
		}
		return response.getBody();
	}
	
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/system/32/31990_2.png) [@system](https://community.cumulocity.com/u/system)\
**Post date:** [September 5, 2023, 11:56am UTC](https://community.cumulocity.com/t/microservice-sdk-java-best-way-to-call-unsupperted-but-available-rest-endpoints/7747/8 "2023-09-05T11:56:33Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
