• Skip to main content
  • Select language
  • Skip to search
MDN Web Docs
  • Technologies
    • HTML
    • CSS
    • JavaScript
    • Graphics
    • HTTP
    • APIs / DOM
    • WebExtensions
    • MathML
  • References & Guides
    • Learn web development
    • Tutorials
    • References
    • Developer Guides
    • Accessibility
    • Game development
    • ...more docs
Add-ons
  1. MDN
  2. Mozilla
  3. Add-ons
  4. Add-on SDK
  5. High-Level APIs
  6. request

request

In This Article
  1. Globals
    1. Constructors
      1. Request(options)
  2. Request
    1. Methods
      1. get()
      2. head()
      3. post()
      4. put()
      5. delete()
    2. Properties
      1. url
      2. headers
      3. content
      4. contentType
      5. response
    3. Events
      1. complete
  3. Response
    1. Properties
      1. url
      2. text
      3. json
      4. status
      5. statusText
      6. headers
      7. anonymous

Add-ons using the techniques described in this document are considered a legacy technology in Firefox. Don't use these techniques to develop new add-ons. Use WebExtensions instead. If you maintain an add-on which uses the techniques described here, consider migrating it to use WebExtensions.

From Firefox 53 onwards, no new legacy add-ons will be accepted on addons.mozilla.org (AMO).

From Firefox 57 onwards, WebExtensions will be the only supported extension type, and Firefox will not load other types.

Even before Firefox 57, changes coming up in the Firefox platform will break many legacy extensions. These changes include multiprocess Firefox (e10s), sandboxing, and multiple content processes. Legacy extensions that are affected by these changes should migrate to WebExtensions if they can. See the "Compatibility Milestones" document for more.

A wiki page containing resources, migration paths, office hours, and more, is available to help developers transition to the new technologies.

Stable

Examples outlined in this document are no longer relevent in regards to the Twitter API calls and need to be updated

Make simple network requests. For more advanced usage, check out the net/xhr module, based on the browser's XMLHttpRequest object.

Globals

Constructors

Request(options)

This constructor creates a request object that can be used to make network requests. The constructor takes a single parameter options which is used to set several properties on the resulting Request.

Parameters

options : object
Optional options:

Name Type  
url string,url

This is the url to which the request will be made. Can either be a String or an instance of the SDK's URL.

onComplete function

This function will be called when the request has received a response (or in terms of XHR, when readyState == 4). The function is passed a Response object.

headers object

An unordered collection of name/value pairs representing headers to send with the request.

content string,object

The content to send to the server. If content is a string, it should be URL-encoded (use encodeURIComponent). If content is an object, it should be a collection of name/value pairs. Nested objects & arrays should encode safely.

For GET and HEAD requests, the query string (content) will be appended to the URL. For POST and PUT requests, it will be sent as the body of the request.

contentType string

The type of content to send to the server. This explicitly sets the Content-Type header. The default value is application/x-www-form-urlencoded.

overrideMimeType string

Use this string to override the MIME type returned by the server in the response's Content-Type header. You can use this to treat the content as a different MIME type, or to force text to be interpreted using a specific character.

For example, if you're retrieving text content which was encoded as ISO-8859-1 (Latin 1), it will be given a content type of "utf-8" and certain characters will not display correctly. To force the response to be interpreted as Latin-1, use overrideMimeType:

var Request = require("sdk/request").Request;
var quijote = Request({
  url: "http://www.latin1files.org/quijote.txt",
  overrideMimeType: "text/plain; charset=latin1",
  onComplete: function (response) {
    console.log(response.text);
  }
});
quijote.get();
anonymous boolean If true, the request will be sent without cookies or authentication headers. This option sets the mozAnon property in the underlying XMLHttpRequest object. Defaults to false.

Request

The Request object is used to make GET, HEAD, POST, PUT, or DELETE network requests. It is constructed with a URL to which the request is sent. Optionally the user may specify a collection of headers and content to send alongside the request and a callback which will be executed once the request completes.

Once a Request object has been created a GET request can be executed by calling its get() method, a POST request by calling its post() method, and so on.

When the server completes the request, the Request object emits a "complete" event. Registered event listeners are passed a Response object.

Each Request object is designed to be used once. Attempts to reuse them will throw an error.

Since the request is not being made by any particular website, requests made here are not subject to the same-domain restriction that requests made in web pages are subject to.

With the exception of response, all of a Request object's properties correspond with the options in the constructor. Each can be set by simply performing an assignment. However, keep in mind that the same validation rules that apply to options in the constructor will apply during assignment. Thus, each can throw if given an invalid value.

The example below shows how to use Request to get the most recent tweet from the @mozhacks account:

var Request = require("sdk/request").Request;
var latestTweetRequest = Request({
  url: "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=mozhacks&count=1",
  onComplete: function (response) {
    var tweet = response.json[0];
    console.log("User: " + tweet.user.screen_name);
    console.log("Tweet: " + tweet.text);
  }
});
// Be a good consumer and check for rate limiting before doing more.
Request({
  url: "https://api.twitter.com/1.1/application/rate_limit_status.json",
  onComplete: function (response) {
    if (response.json.remaining_hits) {
      latestTweetRequest.get();
    } else {
      console.log("You have been rate limited!");
    }
  }
}).get();

Methods

get()

Make a GET request.

head()

Make a HEAD request.

post()

Make a POST request.

put()

Make a PUT request.

delete()

Make a DELETE request.

Properties

url

headers

content

contentType

response

Events

complete

The Request object emits this event when the request has completed and a response has been received.

Arguments

Response : Listener functions are passed the response to the request as a Response object.

Response

The Response object contains the response to a network request issued using a Request object. It is returned by the get(), head(), post(), put() or delete() method of a Request object.

All members of a Response object are read-only.

Properties

url

The URL of the response content.

text

The content of the response as plain text.

json

The content of the response as a JavaScript object. The value will be null if the document cannot be processed by JSON.parse.

status

The HTTP response status code (e.g. 200).

statusText

The HTTP response status line (e.g. OK).

headers

The HTTP response headers represented as key/value pairs.

To print all the headers you can do something like this:

for (var headerName in response.headers) {
  console.log(headerName + " : " + response.headers[headerName]);
}

anonymous

Boolean indicating if the request was anonymous.

Document Tags and Contributors

Tags: 
  • add-on
  • NeedsUpdate
  • SDK
 Contributors to this page: wbamberg, DoomTay, freaktechnik, nikolas, jswisher, KyroChi, eliemichel, 2is10
 Last updated by: wbamberg, Dec 1, 2016, 10:23:47 AM
See also
  1. WebExtensions
  2. Getting started
    1. What are WebExtensions?
    2. Your first WebExtension
    3. Your second WebExtension
    4. Anatomy of a WebExtension
    5. Example WebExtensions
  3. How to
    1. Intercept HTTP requests
    2. Modify a web page
    3. Add a button to the toolbar
    4. Implement a settings page
  4. Concepts
    1. Using the JavaScript APIs
    2. User interface components
    3. Content scripts
    4. Match patterns
    5. Internationalization
    6. Content Security Policy
    7. Native messaging
  5. Porting
    1. Porting a Google Chrome extension
    2. Porting a legacy Firefox add-on
    3. Embedded WebExtensions
    4. Comparison with the Add-on SDK
    5. Comparison with XUL/XPCOM extensions
    6. Chrome incompatibilities
  6. Firefox workflow
    1. Temporary Installation in Firefox
    2. Debugging
    3. Getting started with web-ext
    4. web-ext command reference
    5. WebExtensions and the Add-on ID
    6. Publishing your WebExtension
  7. JavaScript APIs
    1. Browser support for JavaScript APIs
    2. alarms
    3. bookmarks
    4. browserAction
    5. browsingData
    6. commands
    7. contextMenus
    8. contextualIdentities
    9. cookies
    10. downloads
    11. events
    12. extension
    13. extensionTypes
    14. history
    15. i18n
    16. identity
    17. idle
    18. management
    19. notifications
    20. omnibox
    21. pageAction
    22. runtime
    23. sessions
    24. sidebarAction
    25. storage
    26. tabs
    27. topSites
    28. webNavigation
    29. webRequest
    30. windows
  8. Manifest keys
    1. applications
    2. author
    3. background
    4. browser_action
    5. chrome_url_overrides
    6. commands
    7. content_scripts
    8. content_security_policy
    9. default_locale
    10. description
    11. developer
    12. homepage_url
    13. icons
    14. manifest_version
    15. name
    16. omnibox
    17. options_ui
    18. page_action
    19. permissions
    20. short_name
    21. sidebar_action
    22. version
    23. web_accessible_resources
  9. Add-on SDK
  10. Getting started
    1. Installation
    2. Getting started
    3. Troubleshooting
  11. High-Level APIs
    1. addon-page
    2. base64
    3. clipboard
    4. context-menu
    5. hotkeys
    6. indexed-db
    7. l10n
    8. notifications
    9. page-mod
    10. page-worker
    11. panel
    12. passwords
    13. private-browsing
    14. querystring
    15. request
    16. selection
    17. self
    18. simple-prefs
    19. simple-storage
    20. system
    21. tabs
    22. timers
    23. ui
    24. url
    25. webextension
    26. widget
    27. windows
  12. Low-Level APIs
    1. /loader
    2. chrome
    3. console/plain-text
    4. console/traceback
    5. content/content
    6. content/loader
    7. content/mod
    8. content/symbiont
    9. content/worker
    10. core/heritage
    11. core/namespace
    12. core/promise
    13. dev/panel
    14. event/core
    15. event/target
    16. frame/hidden-frame
    17. frame/utils
    18. fs/path
    19. io/byte-streams
    20. io/file
    21. io/text-streams
    22. lang/functional
    23. lang/type
    24. loader/cuddlefish
    25. loader/sandbox
    26. net/url
    27. net/xhr
    28. places/bookmarks
    29. places/favicon
    30. places/history
    31. platform/xpcom
    32. preferences/event-target
    33. preferences/service
    34. remote/child
    35. remote/parent
    36. stylesheet/style
    37. stylesheet/utils
    38. system/child_process
    39. system/environment
    40. system/events
    41. system/runtime
    42. system/unload
    43. system/xul-app
    44. tabs/utils
    45. test/assert
    46. test/harness
    47. test/httpd
    48. test/runner
    49. test/utils
    50. ui/button/action
    51. ui/button/toggle
    52. ui/frame
    53. ui/id
    54. ui/sidebar
    55. ui/toolbar
    56. util/array
    57. util/collection
    58. util/deprecate
    59. util/list
    60. util/match-pattern
    61. util/object
    62. util/uuid
    63. window/utils
  13. Firefox for Android
  14. Getting started
    1. Walkthrough
    2. Debugging
    3. Code snippets
  15. APIs
    1. Accounts.jsm
    2. BrowserApp
    3. HelperApps.jsm
    4. Home.jsm
    5. HomeProvider.jsm
    6. JavaAddonManager.jsm
    7. NativeWindow
    8. Notifications.jsm
    9. PageActions.jsm
    10. Prompt.jsm
    11. RuntimePermissions.jsm
    12. Snackbars.jsm
    13. Sound.jsm
    14. Tab
  16. Legacy
  17. Restartless extensions
    1. Overview
  18. Overlay extensions
    1. Overview
  19. Themes
  20. Lightweight themes
    1. Overview
  21. Complete themes
    1. Overview
  22. Publishing add-ons
  23. Guides
    1. Signing and distribution overview
    2. Submit an add-on
    3. Review policies
    4. Developer agreement
    5. Featured add-ons
    6. Contact addons.mozilla.org
  24. Community and support
  25. Channels
    1. Add-ons blog
    2. Add-on forums
    3. Stack Overflow
    4. Development newsgroup
    5. IRC Channel