Skip to content

Instantly share code, notes, and snippets.

@newtriks
Created February 10, 2014 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save newtriks/8918409 to your computer and use it in GitHub Desktop.
Save newtriks/8918409 to your computer and use it in GitHub Desktop.
Example HTTP POST with JSON body data using Q based on reported issue https://github.com/kriskowal/q-io/issues/8#issuecomment-34633958
var body = JSON.stringify({
token: 'foo',
files: files // Array of filenames
});
var headers = {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body, 'utf8')
};
var request = {
url: apiURL,
charset: 'UTF-8',
method: 'POST',
headers: headers,
body: [body]
};
return http.request(request)
.then(function (response) {
var ok = response.status >= 200 && response.status < 400;
if (!ok) {
throw new Error('API responded with a status code: ' + response.status);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment