Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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