1# HTTP Data Request 2 3## Use Cases 4 5An application can initiate a data request over HTTP. Common HTTP methods include **GET**, **POST**, **OPTIONS**, **HEAD**, **PUT**, **DELETE**, **TRACE**, and **CONNECT**. 6 7## Available APIs 8 9The HTTP request function is mainly implemented by the HTTP module. 10 11To use related APIs, you must declare the **ohos.permission.INTERNET** permission. 12 13The following table describes the related APIs. 14 15| API | Description | 16| ----------------------------------------- | --------------------------------------------------------- | 17| createHttp() | Creates an HTTP request. | 18| request() | Initiates an HTTP request to a given URL. | 19| destroy() | Destroys an HTTP request. | 20| on(type: 'headersReceive') | Registers an observer for HTTP Response Header events. | 21| off(type: 'headersReceive') | Unregisters the observer for HTTP Response Header events. | 22 23## How to Develop 24 251. Import the required HTTP module. 262. Create an **HttpRequest** object. 273. (Optional) Listen for HTTP Response Header events. 284. Initiate an HTTP request to a given URL. 295. (Optional) Process the HTTP Response Header event and the return result of the HTTP request. 30 31```js 32import http from '@ohos.net.http'; 33 34// Each HttpRequest corresponds to an HttpRequestTask object and cannot be reused. 35let httpRequest = http.createHttp(); 36 37// Subscribe to the HTTP response header, which is returned earlier than HttpRequest. You can subscribe to HTTP Response Header events based on service requirements. 38// on('headerReceive', AsyncCallback) will be replaced by on('headersReceive', Callback) in API version 8. 8+ 39httpRequest.on('headersReceive', (header) => { 40 console.info('header: ' + JSON.stringify(header)); 41}); 42 43httpRequest.request( 44 // Set the URL of the HTTP request. You need to define the URL. Set the parameters of the request in extraData. 45 "EXAMPLE_URL", 46 { 47 method: http.RequestMethod.POST, // Optional. The default value is http.RequestMethod.GET. 48 // You can add the header field based on service requirements. 49 header: { 50 'Content-Type': 'application/json' 51 }, 52 // This field is used to transfer data when the POST request is used. 53 extraData: { 54 "data": "data to send", 55 }, 56 connectTimeout: 60000, // Optional. The default value is 60000, in ms. 57 readTimeout: 60000, // Optional. The default value is 60000, in ms. 58 }, (err, data) => { 59 if (!err) { 60 // data.result contains the HTTP response. Parse the response based on service requirements. 61 console.info('Result:' + data.result); 62 console.info('code:' + data.responseCode); 63 // data.header contains the HTTP response header. Parse the content based on service requirements. 64 console.info('header:' + JSON.stringify(data.header)); 65 console.info('cookies:' + data.cookies); // 8+ 66 } else { 67 console.info('error:' + JSON.stringify(err)); 68 // Call the destroy() method to release resources after the call is complete. 69 httpRequest.destroy(); 70 } 71 } 72); 73``` 74