Created
February 10, 2014 15:58
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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