The following examples show how to make HTTP requests using the HttpRequest class.

Simple Get Request


Make a Get request to a server and retrieve the status code, the response headers, and the response text body:

response = HttpRequest.Get('https://httpbin.org/')
print, response.status_code
print, response.headers
print, response.text

Get Request with Parameters


Here we send a couple of parameters to a server. Since our parameters contain disallowed characters (such as spaces) we use the ESCAPE keyword:

params = hash('param1', 'my value', 'param2', -123.4)
response = HttpRequest.Get('https://httpbin.org/', params=params, /escape)
print, response.url

IDL prints:

http://myserver.com/?param1=my%20value¶m2=-123.400

Tip: You can also use the HttpRequest.Escape and Unescape methods to convert strings with disallowed or special characters.

Get Request with Basic Authentication


Make a request using basic authentication:

basic = 'Basic ' + idl_base64(byte('myname:mypass'))
response = HTTPRequest.Get('myserver.org', headers=hash('Authorization', basic))
print, `status_code = ${response.status_code}`

Note: This is a fake URL so this example will not actually run; you will need to use your own URL and the correct username and password.

Handling Errors


Make a request to a nonexistent server. In this case you will get an internal CURL error (response code < 100) and the error message will be in the response.text field:

response = HTTPRequest.Get('http://myfancyserver.org')
print, `status_code = ${response.status_code}`
print, response.text

IDL prints:

status_code = 6
Could not resolve host: myfancyserver.org

Now make a request to a real server but a nonexistent path. In this case you will get a 404 response code and the server's response will be in the response.text field:

response = HTTPRequest.Get('http://google.com/mybadpath')
print, `status_code = ${response.status_code}`
print, response.text

IDL prints:

status_code = 404
<!DOCTYPE html>
<html lang=en>
...
<a href=//www.google.com/><span id=logo aria-label=Google></span></a>
<p><b>404.</b> <ins>That's an error.</ins>
<p>The requested URL <code>/mybadpath</code> was not found on this server.  <ins>That's all we know.</ins>

Post Request with Key/Value Pairs


Make a post request, passing in two key/value pairs as parameters:

params = dictionary('key1', 'value1', 'key2', 'value2')
response = HTTPRequest.Post('https://httpbin.org/post', params=params)
print, `status_code = ${response.status_code}`
print, response.text

IDL prints:

status_code = 200
{
  ...
  "form": {
    "key1": "value1",
    "key2": "value2"
  },
  "headers": {
    "Accept": "*/*",
    "Content-Length": "23",
    "Content-Type": "application/x-www-form-urlencoded",
    ...
  },
  ...
}

Post Request with JSON Data


Make a post request and pass in two key/value pairs as JSON data. The response body is in JSON format so we use the HttpRequest.Json method:

formdata = dictionary('key1', 'value1', 'key2', 'value2')
response = HTTPRequest.Post('https://httpbin.org/post', json=formdata)
print, response.json(), /implied

IDL prints:

{
  "data": "{\"key2\":\"value2\",\"key1\":\"value1\"}",
  "headers": {
    "Accept": "*/*",
    "Content-Length": "33",
    "Content-Type": "application/json",
    ...
  },
  "json": {
    "key1": "value1",
    "key2": "value2"
  },
  ...
}

Post Request with Multipart Form and Files


Make a post request with a multipart form that includes a simple string, some binary data (with and without a mime type), and two files (one with a mime type):

myfile = filepath('md1107g8a.jpg', subdir=['examples', 'data'])
myformdata = dictionary('name', 'Dave Stern', $
  'mydata1', bindgen(10), $
  'mydata2', {value: bindgen(10), mimetype: 'application/octet-stream'}, $
  'myimage1', {file: myfile}, $
  'myimage1', {file: myfile, mimetype: 'image/jpeg'})
response = HTTPRequest.Post('https://myserver.org/', multipart=myformdata)

Note: This is a fake URL so this example will not actually run; it is only here to show how to pass multipart data.

Get Request with a Callback


First we define a callback function that will print out a progress status. Our callback also looks for a keypress: if you press "q" then the function will return zero and the request will be canceled.

function mycallback, downTotal, downNow, upTotal, upNow, callbackData
  compile_opt idl2
  if (get_kbrd(0) eq 'q') then return, 0
  percent = round(100 * float(downNow) / (downTotal > 1))
  if (callbackData eq percent) then return, 1
  callbackData = percent
  if (percent gt 0) then print, `${percent}% ${('*').dup(stars)}`
  return, 1
end

Now download a large file, making sure to pass in our callback function and our initial percentage:

response = HTTPRequest.Get('http://mattmahoney.net/dc/enwik8.zip', $
  CALLBACK_FUNCTION='mycallback', CALLBACK_DATA=0)

IDL prints:

1% *
...
50% **************************************************
...
100% ****************************************************************************************************

Version History


9.0

Introduced

See Also


HttpRequest