# HTTPRequest example

**URL:** https://resource.dopus.com/t/httprequest-example/50867
**Category:** Buttons/Scripts
**Created:** [May 20, 2024, 8:51am UTC](https://resource.dopus.com/t/httprequest-example/50867 "2024-05-20T08:51:07Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Jon](https://resource.dopus.com/user_avatar/resource.dopus.com/jon/32/12713_2.png) [@Jon](https://resource.dopus.com/u/Jon)
#### Post date: [May 20, 2024, 8:51am UTC](https://resource.dopus.com/t/httprequest-example/50867/1 "2024-05-20T08:51:07Z")

</div>

Here's a simple example of using the new [HTTPRequest](https://docs.dopus.com/doku.php?id=reference:scripting_reference:scripting_objects:httprequest) object added in [13.6.2](https://resource.dopus.com/t/directory-opus-13-6-2-beta/50865).

It opens a dialog and launches three asynchronous requests for images, and displays them when received.

Example button:  
[HTTP Example.dcf](https://resource.dopus.com/uploads/short-url/uMKiBhP2sPeB1RVjsNMm42Mnimb.dcf) (3.3 KB)

Script:

````javascript
function OnClick(clickData)
{
	var Dlg = DOpus.Dlg;
	Dlg.window = clickData.func.sourcetab;
	Dlg.template = "testdlg";
	Dlg.detach = true;
	Dlg.Show();

	var Reqs = {};
	for (var i = 0; i < 3; i++)
	{
		var newReq = Dlg.NewHTTPReq();
		newReq.SendRequest("https://www.gpsoft.com.au/assets/img/product-screen-" + (i + 1) + ".jpg");
		Reqs[newReq.id] = { req: newReq, ctrl: "static" + (i + 1) };
	}

	while (true)
	{
	    var Msg = Dlg.GetMsg();
    	if (!Msg.result) break;

		if (Msg.event == "http" && Msg.value == "data")
		{
			var Req = Reqs[Msg.control];
			if (!Req.req.complete)
			{
				var reqData = Req.req.ReadResponse();
				if (DOpus.TypeOf(reqData) == "object.Image")
					Dlg.Control(Req.ctrl).title = reqData;
				Req.req.shutdown();
			}
		}
	}
}
```
````

---

<div class="post-metadata">

### Author: ![errante](https://resource.dopus.com/user_avatar/resource.dopus.com/errante/32/61692_2.png) [@errante](https://resource.dopus.com/u/errante)
#### Post date: [May 21, 2024, 4:16am UTC](https://resource.dopus.com/t/httprequest-example/50867/2 "2024-05-21T04:16:39Z")

</div>

Again many thanks for including this.  
A few questions after playing with `HTTPRequest` a bit, but using POST:

1. Before , I used HTTPRequest (via ActiveX) with a structure like this as the data to send:

2. Once the send is performed with `SendRequest()`, should `shutdown()` stop the http event or is there another way?  
I tried something like this (just the loop for the dialog) :

---

<div class="post-metadata">

### Author: ![Jon](https://resource.dopus.com/user_avatar/resource.dopus.com/jon/32/12713_2.png) [@Jon](https://resource.dopus.com/u/Jon)
#### Post date: [May 23, 2024, 9:26pm UTC](https://resource.dopus.com/t/httprequest-example/50867/3 "2024-05-23T21:26:22Z")

</div>

> [@errante](#):
>
> But after `shutdown()` is executed, the event for `http` keeps triggering and gives an error in `xhr.errorcode`, since many properties/methods are no longer avaialble.

As in the example, you should check the `complete` property before using any other methods or properties if you're calling `Shutdown()`. Messages could still be in the message queue even after the request is terminated.
