Problem using hyper

Hello!

I would like to use cfml http (hyper) in a Coldbox 7.x and unfortunately i get the following error.

In my Application.cfc i have declared manually the following
this.mappings[ "/hyper" ] = expandPath( "/modules/hyper" );

and additionally i have also used the following code via cbwire (cfm page)

 property name="hyper" inject="HyperBuilder@Hyper";

 HyperResponse function getNotice(){
    
         return hyper.new().post(
                            url="https://cerpp.eprocurement.gov.gr/khmdhs-opendata/notice",
                            body ={
                                "referenceNumber": "25PROC016359713"        
                            });

        }
<cfdump var="#getNotice()#">

Any idea what should i do?

Regards

ColdBox will load Hyper module (https://hyper.ortusbooks.com/) automatically. Just remove the /hyper mapping in Application.cfc.

handlers/Test.cfc

component extends="coldbox.system.EventHandler" {
	property name="hyper" inject="HyperBuilder@hyper";

	function index(event, rc, prc) {
		var response = hyper
			.setMethod( "POST" )
			.setUrl( "https://cerpp.eprocurement.gov.gr/khmdhs-opendata/notice" )
			.setBody({
				"referenceNumber": "25PROC016359713"
			})
			.send();
		var payload = response.json();

		dump(payload);

		event.noRender();
	}
}

Is that possible via cbwire or not and how?

Regards again

Do cbwire pages get loaded by WireBox? If not, there is no dependency injection.

Not knowing a ton about cbwire (ping @gcopley) it seems that this function should go in the class/component, not the template.

1 Like

My experience in cbWire is limited. The following is a quick dirty example to demonstrate Hyper via cbWire.

// ./wires/Counter.cfc
component extends="cbwire.models.Component" {
	property name="hyper" inject="HyperBuilder@hyper";
	// Data properties
	data = {
		"title": "-",
		"counter": 0 // default value
	};
	// Action
	function increment() {
		var response = hyper
			.setMethod( "POST" )
			.setUrl( "https://cerpp.eprocurement.gov.gr/khmdhs-opendata/notice" )
			.setBody({
				"referenceNumber": "25PROC016359713"
			})
			.send();
		var payload = response.json();
		data.title = payload.content[1].title;

		data.counter++;
	}
	// Helper method also available to template
	function isEven() {
		return data.counter % 2 == 0;
	}
}
<!--- ./wires/counter.cfm --->
<cfoutput>
<!--- IMPORTANT: Wrapping with div tag is required --->
<div>
	<h1>My Counter</h1>
	Title: #title#<br>
	Counter: #counter#<br>
	Is Even: #isEven()#
	<button wire:click="increment">Increment</button>
</div>
</cfoutput>
<!--- ./views/test/index.cfm --->
<cfoutput>
	#wire( name="Counter" )#
</cfoutput>

Give a second or two to display ‘title’.

Good luck!

1 Like

Yes, you should not need any special configuration for Hyper to work in a wire. When a wire is called cbWire mounts your wire and wirebox will handle the dependency injection for you.

Just keep in mind, you can’t reference hyper (the injected HyperBuilder) in the template (or template area if you are using a single file wire). You CAN use it anywhere in the component code directly.

Also, you have access to wirebox directly in your wire component. Instead of injecting using the property annotation you could also just get it when you need it and leave out the property name="hyper" inject="HyperBuilder@hyper";

		var response = wirebox.getInstance("HyperBuilder@hyper")
			.setMethod( "POST" )
			.setUrl( "https://cerpp.eprocurement.gov.gr/khmdhs-opendata/notice" )
			.setBody({
				"referenceNumber": "25PROC016359713"
			})
			.send();

If you are still having trouble try sharing the entire wire.

1 Like

@asimkon Yep, what @MikeR said :slight_smile:

You won’t be able to call it directly from your CBWIRE template but that’s by design. You can use property injection in your wire component CFC or you can call getInstance() in any of your methods.

1 Like

Thanks a lot for your assistance!