<commit_before># uhttp

A micro ajax library modeled after angularjs's $http module that doesn't require any dependencies (jquery or otherwise). As of version 1.1.0, uhttp runs in browsers as well as in nodejs.

uhttp is about 6kb minified and about 2.3kb minified and compressed.

uhttp supports setting headers globally for all requests and setting headers individually for each request. It also automatically parses json in responses with the appropriate content type. uhttp is based off of [atomic](https://github.com/toddmotto/atomic) and angularjs's [$http](https://github.com/angular/angular.js/blob/v1.3.x/src/ng/http.js). uhttp was written because atomic didn't support common features (setting headers & sending json POST requests) and [React](https://facebook.github.io/react/index.html) didn't come with a built in ajax library (recommending jQuery's ajax instead).

Note that uhttp does not use true [promises](https://github.com/jakearchibald/es6-promise).

#### Downloading & Setting up

Download the minified build [here](https://raw.githubusercontent.com/Flux159/uhttp/master/dist/uhttp.min.js), put into your public scripts directory, and add to your webpage by adding the following tag:

```
<script type="application/javascript" src="/scripts/uhttp.min.js"></script>
```

Alternatively, you can install from bower as well:
```
bower install uhttp --save
```

To install with nodejs, run the following:
```
npm install uhttp --save
```

Use server side by requiring in your files:
```javascript
var uhttp = require('uhttp');
```

#### uhttp.get(url, [,options])
Use uhttp.get() to make a GET request. You can use "then... catch" callbacks to obtain the response.

```javascript
uhttp.get('/api/endpoint').then(function(res, status, xhr) {
    //Successful response
}).catch(function(err, status, xhr) {
    //Error
});

```

You can specify options by passing an options object to any request (see the Options section for more information):

```javascript
var options = {
    headers: {
        'custom': 'header'
    },
    withCredentials: true,
    timeout: 3000 //3 seconds; '0' for no timeout
};

uhttp.get('/api/endpoint', options).then(function(res, status, xhr) {
    //Success
}).catch(function(err, status, xhr) {
    //Error
});

```

#### uhttp.post(url, [,options] [,data])

Use uhttp.post() to make a POST request. By default, the content type for JSON and FormData (multipart/form-data NOT application/x-www-form-urlencoded) is automatically set for you. To change this, either set a global default for the "Content-Type" header, or pass it in as an option.

```javascript
uhttp.post('/api/endpoint/post', {some: 'data'}).then(function(res) {
    //Success
}).catch(function(err) {
    //Error
});
```

Example using FormData (Content-Type: multipart/form-data) in javascript:
```javascript
var formElement = document.getElementById("myform");
var formData = new FormData(formElement);
formData.append("username", "cat");
uhttp.post('/api/endpoint/post/form/multipart', formData).then(function(data) {
    //Success
}).catch(function(err) {
    //Error
});
```

##### Posting x-www-form-urlencoded data

Here is an example of posting x-www-form-urlencoded data. Note that if you want all requests sent with these options by default, use uhttp.setGlobalOptions (see documentation below).

```javascript
var data = {
    content: 'Sending json object as x-www-form-urlencoded'
};

var options = {
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    transformRequestData: function(data) {
        var str = [];
        for(var p in data) {
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(data[p]));
        }
        return str.join("&");
}};

uhttp.post('/api/endpoint/post/form/urlencoded', options, data).then(function(res) {
    //Success
}).catch(function(err) {
    //Error
});
```

Example setting a custom content type:
```javascript
var myCustomData = '<custom>xml</custom>';
uhttp.post('/api/endpoint/post', {'Content-Type': 'application/xml'}, myCustomData).then(function(res, status, xhr) {
    //Success
}).catch(function(err, status, xhr) {
    //Error
});
```

#### uhttp.put()

Use uhttp.put() to make a PUT request. The options are similar to POST requests.

```javascript
uhttp.put('/api/endpoint/put', {some: 'data'}).then(function(res, status, xhr) {
    //Success
}).catch(function(err, status, xhr) {
    //Error
});
```

#### uhttp.patch()

Use uhttp.patch() to make a PATCH request.

```javascript
uhttp.patch('/api/endpoint/patch', {some: 'data'}).then(function(res, status, xhr) {
    //Success
}).catch(function(err, status, xhr) {
    //Error
});
```

#### uhttp.delete()

Use uhttp.delete() to send a DELETE request.

```javascript
uhttp.delete('/api/endpoint/delete').then(function(res, status, xhr) {
    //Success
}).catch(function(err, status, xhr) {
    //Error
});
```

#### uhttp.head()

Use uhttp.head() to send a HEAD request.

```javascript
uhttp.head('/api/endpoint/head').then(function(res, status, xhr) {
    //Success
    console.log(xhr.getHeader('Custom-Header'));
}).catch(function(err, status, xhr) {
    //Error
});
```

#### uhttp.jsonp()

Use uhttp.jsonp() to send a JSONP request. Note that you should define the callback as 'JSON_CALLBACK'. uhttp will generate a global function attached to the window object for the duration of the request and pass its data to the then/catch functions.

```javascript
uhttp.jsonp('/api/endpoint/jsonp?callback=JSON_CALLBACK').then(function(res, status, xhr) {
    //Success
}).catch(function(res, status, xhr) {
    //Error
});
```

#### Xsrf
uhttp handles setting an XSRF header for you based on the options xsrfCookieName and xsrfHeaderName options. The default options are cookieName = 'XSRF-TOKEN' and headerName = 'X-XSRF-TOKEN'. Note that uhttp takes the value of the cookie (not the name) and sets the header given by xsrfHeaderName to that value. To change these, specify the options before sending your requests:

```javascript
var globalOptions = {
    xsrfCookieName: 'CUSTOM-COOKIE-NAME',
    xsrfHeaderName: 'CUSTOM-HEADER-NAME'
};

uhttp.setGlobalOptions = globalOptions;

uhttp.get('/xsrf/endpoint').then(function(res) {<commit_msg>Adding more info for jsonp.<commit_after># uhttp

A micro ajax library modeled after angularjs's $http module that doesn't require any dependencies (jquery or otherwise). As of version 1.1.0, uhttp runs in browsers as well as in nodejs.

uhttp is about 6kb minified and about 2.3kb minified and compressed.

uhttp supports setting headers globally for all requests and setting headers individually for each request. It also automatically parses json in responses with the appropriate content type. uhttp is based off of [atomic](https://github.com/toddmotto/atomic) and angularjs's [$http](https://github.com/angular/angular.js/blob/v1.3.x/src/ng/http.js). uhttp was written because atomic didn't support common features (setting headers & sending json POST requests) and [React](https://facebook.github.io/react/index.html) didn't come with a built in ajax library (recommending jQuery's ajax instead).

Note that uhttp does not use true [promises](https://github.com/jakearchibald/es6-promise).

#### Downloading & Setting up

Download the minified build [here](https://raw.githubusercontent.com/Flux159/uhttp/master/dist/uhttp.min.js), put into your public scripts directory, and add to your webpage by adding the following tag:

```
<script type="application/javascript" src="/scripts/uhttp.min.js"></script>
```

Alternatively, you can install from bower as well:
```
bower install uhttp --save
```

To install with nodejs, run the following:
```
npm install uhttp --save
```

Use server side by requiring in your files:
```javascript
var uhttp = require('uhttp');
```

#### uhttp.get(url, [,options])
Use uhttp.get() to make a GET request. You can use "then... catch" callbacks to obtain the response.

```javascript
uhttp.get('/api/endpoint').then(function(res, status, xhr) {
    //Successful response
}).catch(function(err, status, xhr) {
    //Error
});

```

You can specify options by passing an options object to any request (see the Options section for more information):

```javascript
var options = {
    headers: {
        'custom': 'header'
    },
    withCredentials: true,
    timeout: 3000 //3 seconds; '0' for no timeout
};

uhttp.get('/api/endpoint', options).then(function(res, status, xhr) {
    //Success
}).catch(function(err, status, xhr) {
    //Error
});

```

#### uhttp.post(url, [,options] [,data])

Use uhttp.post() to make a POST request. By default, the content type for JSON and FormData (multipart/form-data NOT application/x-www-form-urlencoded) is automatically set for you. To change this, either set a global default for the "Content-Type" header, or pass it in as an option.

```javascript
uhttp.post('/api/endpoint/post', {some: 'data'}).then(function(res) {
    //Success
}).catch(function(err) {
    //Error
});
```

Example using FormData (Content-Type: multipart/form-data) in javascript:
```javascript
var formElement = document.getElementById("myform");
var formData = new FormData(formElement);
formData.append("username", "cat");
uhttp.post('/api/endpoint/post/form/multipart', formData).then(function(data) {
    //Success
}).catch(function(err) {
    //Error
});
```

##### Posting x-www-form-urlencoded data

Here is an example of posting x-www-form-urlencoded data. Note that if you want all requests sent with these options by default, use uhttp.setGlobalOptions (see documentation below).

```javascript
var data = {
    content: 'Sending json object as x-www-form-urlencoded'
};

var options = {
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    transformRequestData: function(data) {
        var str = [];
        for(var p in data) {
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(data[p]));
        }
        return str.join("&");
}};

uhttp.post('/api/endpoint/post/form/urlencoded', options, data).then(function(res) {
    //Success
}).catch(function(err) {
    //Error
});
```

Example setting a custom content type:
```javascript
var myCustomData = '<custom>xml</custom>';
uhttp.post('/api/endpoint/post', {'Content-Type': 'application/xml'}, myCustomData).then(function(res, status, xhr) {
    //Success
}).catch(function(err, status, xhr) {
    //Error
});
```

#### uhttp.put()

Use uhttp.put() to make a PUT request. The options are similar to POST requests.

```javascript
uhttp.put('/api/endpoint/put', {some: 'data'}).then(function(res, status, xhr) {
    //Success
}).catch(function(err, status, xhr) {
    //Error
});
```

#### uhttp.patch()

Use uhttp.patch() to make a PATCH request.

```javascript
uhttp.patch('/api/endpoint/patch', {some: 'data'}).then(function(res, status, xhr) {
    //Success
}).catch(function(err, status, xhr) {
    //Error
});
```

#### uhttp.delete()

Use uhttp.delete() to send a DELETE request.

```javascript
uhttp.delete('/api/endpoint/delete').then(function(res, status, xhr) {
    //Success
}).catch(function(err, status, xhr) {
    //Error
});
```

#### uhttp.head()

Use uhttp.head() to send a HEAD request.

```javascript
uhttp.head('/api/endpoint/head').then(function(res, status, xhr) {
    //Success
    console.log(xhr.getHeader('Custom-Header'));
}).catch(function(err, status, xhr) {
    //Error
});
```

#### uhttp.jsonp()

Use uhttp.jsonp() to send a JSONP request. Note that you should define the callback as 'JSON_CALLBACK'. uhttp will generate a global function attached to the window object for the duration of the request and pass its data to the then/catch functions. Note that since [jsonp](http://en.wikipedia.org/wiki/JSONP) requests use a script element and not xhr, there is no status or xhr returned in the callback.

```javascript
uhttp.jsonp('/api/endpoint/jsonp?callback=JSON_CALLBACK').then(function(res) {
    //Success
}).catch(function(err) {
    //Error
});
```

#### Xsrf
uhttp handles setting an XSRF header for you based on the options xsrfCookieName and xsrfHeaderName options. The default options are cookieName = 'XSRF-TOKEN' and headerName = 'X-XSRF-TOKEN'. Note that uhttp takes the value of the cookie (not the name) and sets the header given by xsrfHeaderName to that value. To change these, specify the options before sending your requests:

```javascript
var globalOptions = {
    xsrfCookieName: 'CUSTOM-COOKIE-NAME',
    xsrfHeaderName: 'CUSTOM-HEADER-NAME'
};

uhttp.setGlobalOptions = globalOptions;

uhttp.get('/xsrf/endpoint').then(function(res) {
{'fraction_non_alphanumeric': 0.14637254121207294, 'fraction_numerical': 0.002866926813729394, 'mean_word_length': 4.225967540574282, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 10, 'lorem ipsum': 0, 'www.': 10, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6900247', 'n_tokens_mistral': 3917, 'n_tokens_neox': 3797, 'n_words': 1236}