I'm using the sandbox API at the moment, and I can query the products, including individually, but if I try and place a buy order, the response I get is { message: 'Product not found' }.
Here's my code:
async function cb_request( method, path, headers = {}, body = ''){
var apiKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
apiSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
apiPass = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx';
//get unix time in seconds
var timestamp = Math.floor(Date.now() / 1000);
// set the request message
var message = timestamp + method + path + body;
//create a hexedecimal encoded SHA256 signature of the message
var key = Buffer.from(apiSecret, 'base64');
var signature = crypto.createHmac('sha256', key).update(message).digest('base64');
//create the request options object
var baseUrl = 'https://api-public.sandbox.pro.coinbase.com';
headers = Object.assign({},headers,{
'CB-ACCESS-SIGN': signature,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': apiKey,
'CB-ACCESS-PASSPHRASE': apiPass,
'USER-AGENT': 'request'
});
// Logging the headers here to ensure they're sent properly
console.log(headers);
var options = {
baseUrl: baseUrl,
url: path,
method: method,
headers: headers
};
return new Promise((resolve,reject)=>{
request( options, function(err, response, body){
if (err) reject(err);
resolve(JSON.parse(response.body));
});
});
}
async function main() {
// This queries a product by id (successfully)
try {
console.log( await cb_request('GET','/products/BTC-USD') );
}
catch(e) {
console.log(e);
}
// Trying to place a buy order here (using the same id as above) returns { message: 'Product not found' }
var buyParams = {
'type': 'market',
'side': 'buy',
'funds': '100',
'product_id': 'BTC-USD'
};
try {
var buy = await cb_request('POST','/orders',buyParams);
console.log(buy);
}
catch(e) {
console.log(e);
}
}
main();
I've tried sending the params in the body, which responds with invalid signature, even when stringified. I've also tried using the params shown in the API docs, but that responds with product not found too.
Any ideas? TIA
As j-petty mentioned you need to send data as request body for POST operation as described in the API documentation so this is why you get "product not found".
Here is working code based on what your shared:
var crypto = require('crypto');
var request = require('request');
async function cb_request( method, path, headers = {}, body = ''){
var apiKey = 'xxxxxx',
apiSecret = 'xxxxxxx',
apiPass = 'xxxxxxx';
//get unix time in seconds
var timestamp = Math.floor(Date.now() / 1000);
// set the request message
var message = timestamp + method + path + body;
console.log('######## message=' + message);
//create a hexedecimal encoded SHA256 signature of the message
var key = Buffer.from(apiSecret, 'base64');
var signature = crypto.createHmac('sha256', key).update(message).digest('base64');
//create the request options object
var baseUrl = 'https://api-public.sandbox.pro.coinbase.com';
headers = Object.assign({},headers,{
'content-type': 'application/json; charset=UTF-8',
'CB-ACCESS-SIGN': signature,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': apiKey,
'CB-ACCESS-PASSPHRASE': apiPass,
'USER-AGENT': 'request'
});
// Logging the headers here to ensure they're sent properly
console.log(headers);
var options = {
'baseUrl': baseUrl,
'url': path,
'method': method,
'headers': headers,
'body': body
};
return new Promise((resolve,reject)=>{
request( options, function(err, response, body){
console.log(response.statusCode + " " + response.statusMessage);
if (err) reject(err);
resolve(JSON.parse(response.body));
});
});
}
async function main() {
// This queries a product by id (successfully)
try {
console.log('try to call product------->');
console.log( await cb_request('GET','/products/BTC-USD') );
console.log('product------------------->done');
}
catch(e) {
console.log(e);
}
var buyParams = JSON.stringify({
'type': 'market',
'side': 'buy',
'funds': '10',
'product_id': 'BTC-USD'
});
try {
console.log('try to call orders------->');
var buy = await cb_request('POST','/orders', {}, buyParams);
console.log(buy);
console.log('orders----------------------->done');
}
catch(e) {
console.log(e);
}
}
main();
You need to send a POST request to the /orders endpoint and include the body in the request payload.
There are some example answers in this question.
var options = {
baseUrl: baseUrl,
url: path,
method: method,
headers: headers
json: true,
body: body
}
request.post(options, function(err, response, body){
if (err) reject(err);
resolve(JSON.parse(response.body));
});
It's worth mentioning that the sandbox API has different results than the production API. Consider the following CURLs.
Sandbox API:
❯ curl --request GET \
--url https://api-public.sandbox.exchange.coinbase.com/products/ETH-USD \
--header 'Accept: application/json'
{"message":"NotFound"}%
Production API:
❯ curl --request GET \
--url https://api.exchange.coinbase.com/products/ETH-USD \
--header 'Accept: application/json'
{"id":"ETH-USD","base_currency":"ETH","quote_currency":"USD","base_min_size":"0.00029","base_max_size":"2800","quote_increment":"0.01","base_increment":"0.00000001","display_name":"ETH/USD","min_market_funds":"1","max_market_funds":"4000000","margin_enabled":false,"fx_stablecoin":false,"max_slippage_percentage":"0.02000000","post_only":false,"limit_only":false,"cancel_only":false,"trading_disabled":false,"status":"online","status_message":"","auction_mode":false}%
You'll notice that the paths are identical but you get different results so keep that in mind. For testing purposes BTC-USD can be used.
Related
I'm using the sandbox API at the moment, and I can query the products, including individually, but if I try and place a buy order, the response I get is { message: 'Product not found' }.
Here's my code:
async function cb_request( method, path, headers = {}, body = ''){
var apiKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
apiSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
apiPass = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx';
//get unix time in seconds
var timestamp = Math.floor(Date.now() / 1000);
// set the request message
var message = timestamp + method + path + body;
//create a hexedecimal encoded SHA256 signature of the message
var key = Buffer.from(apiSecret, 'base64');
var signature = crypto.createHmac('sha256', key).update(message).digest('base64');
//create the request options object
var baseUrl = 'https://api-public.sandbox.pro.coinbase.com';
headers = Object.assign({},headers,{
'CB-ACCESS-SIGN': signature,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': apiKey,
'CB-ACCESS-PASSPHRASE': apiPass,
'USER-AGENT': 'request'
});
// Logging the headers here to ensure they're sent properly
console.log(headers);
var options = {
baseUrl: baseUrl,
url: path,
method: method,
headers: headers
};
return new Promise((resolve,reject)=>{
request( options, function(err, response, body){
if (err) reject(err);
resolve(JSON.parse(response.body));
});
});
}
async function main() {
// This queries a product by id (successfully)
try {
console.log( await cb_request('GET','/products/BTC-USD') );
}
catch(e) {
console.log(e);
}
// Trying to place a buy order here (using the same id as above) returns { message: 'Product not found' }
var buyParams = {
'type': 'market',
'side': 'buy',
'funds': '100',
'product_id': 'BTC-USD'
};
try {
var buy = await cb_request('POST','/orders',buyParams);
console.log(buy);
}
catch(e) {
console.log(e);
}
}
main();
I've tried sending the params in the body, which responds with invalid signature, even when stringified. I've also tried using the params shown in the API docs, but that responds with product not found too.
Any ideas? TIA
As j-petty mentioned you need to send data as request body for POST operation as described in the API documentation so this is why you get "product not found".
Here is working code based on what your shared:
var crypto = require('crypto');
var request = require('request');
async function cb_request( method, path, headers = {}, body = ''){
var apiKey = 'xxxxxx',
apiSecret = 'xxxxxxx',
apiPass = 'xxxxxxx';
//get unix time in seconds
var timestamp = Math.floor(Date.now() / 1000);
// set the request message
var message = timestamp + method + path + body;
console.log('######## message=' + message);
//create a hexedecimal encoded SHA256 signature of the message
var key = Buffer.from(apiSecret, 'base64');
var signature = crypto.createHmac('sha256', key).update(message).digest('base64');
//create the request options object
var baseUrl = 'https://api-public.sandbox.pro.coinbase.com';
headers = Object.assign({},headers,{
'content-type': 'application/json; charset=UTF-8',
'CB-ACCESS-SIGN': signature,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': apiKey,
'CB-ACCESS-PASSPHRASE': apiPass,
'USER-AGENT': 'request'
});
// Logging the headers here to ensure they're sent properly
console.log(headers);
var options = {
'baseUrl': baseUrl,
'url': path,
'method': method,
'headers': headers,
'body': body
};
return new Promise((resolve,reject)=>{
request( options, function(err, response, body){
console.log(response.statusCode + " " + response.statusMessage);
if (err) reject(err);
resolve(JSON.parse(response.body));
});
});
}
async function main() {
// This queries a product by id (successfully)
try {
console.log('try to call product------->');
console.log( await cb_request('GET','/products/BTC-USD') );
console.log('product------------------->done');
}
catch(e) {
console.log(e);
}
var buyParams = JSON.stringify({
'type': 'market',
'side': 'buy',
'funds': '10',
'product_id': 'BTC-USD'
});
try {
console.log('try to call orders------->');
var buy = await cb_request('POST','/orders', {}, buyParams);
console.log(buy);
console.log('orders----------------------->done');
}
catch(e) {
console.log(e);
}
}
main();
You need to send a POST request to the /orders endpoint and include the body in the request payload.
There are some example answers in this question.
var options = {
baseUrl: baseUrl,
url: path,
method: method,
headers: headers
json: true,
body: body
}
request.post(options, function(err, response, body){
if (err) reject(err);
resolve(JSON.parse(response.body));
});
It's worth mentioning that the sandbox API has different results than the production API. Consider the following CURLs.
Sandbox API:
❯ curl --request GET \
--url https://api-public.sandbox.exchange.coinbase.com/products/ETH-USD \
--header 'Accept: application/json'
{"message":"NotFound"}%
Production API:
❯ curl --request GET \
--url https://api.exchange.coinbase.com/products/ETH-USD \
--header 'Accept: application/json'
{"id":"ETH-USD","base_currency":"ETH","quote_currency":"USD","base_min_size":"0.00029","base_max_size":"2800","quote_increment":"0.01","base_increment":"0.00000001","display_name":"ETH/USD","min_market_funds":"1","max_market_funds":"4000000","margin_enabled":false,"fx_stablecoin":false,"max_slippage_percentage":"0.02000000","post_only":false,"limit_only":false,"cancel_only":false,"trading_disabled":false,"status":"online","status_message":"","auction_mode":false}%
You'll notice that the paths are identical but you get different results so keep that in mind. For testing purposes BTC-USD can be used.
I was using Azure Speech rest api. And i tried it on post man with a .wav file and it successfully return the result. However, when i call api from my node.js code. It always return Unsupported Audio Format even though i give the same audio file. Can anyone tell me what's the difference of them? Or what did Postman do to make it work?
Below is how i call speech api by node.js.
'use strict';
const request = require('request');
const subscriptionKey = 'MYSUBSCRIPTIONKEY';
const uriBase = 'https://westus.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US';
const options = {
uri: uriBase,
body: 'speech.wav',
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key' : subscriptionKey,
'Transfer-Encoding': 'chunked',
'Expect': '100-continue',
'Content-type':'audio/wav; codec=audio/pcm; samplerate=16000'
}
};
request.post(options, (error, response, body) => {
if (error) {
console.log('Error: ', error);
return;
}
let jsonResponse = JSON.stringify(JSON.parse(body), null, ' ');
console.log('JSON Response\n');
console.log(jsonResponse);
});
You can try this
fs.readFile('/path/to/my/audiofile.wav', function (err, data) {
if (err) throw err;
var options = {
host: 'https://westus.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US',
method: 'POST',
headers: { 'Content-Type': 'audio/wav' }
};
var req = http.request(options, function(res) {
// Handle a successful response here...
});
req.on('error', function(e) {
// Handle an error response here...
});
// Write the audio data in the request body.
req.write(data);
req.end();
});
I'm trying to send a post request with header in koa.js routes like this:
Here is request function
const request = require('request').defaults({
json: true
});
function *requestPromise(url, method, header, body) {
return new Promise(function (resolve, reject) {
delete header["content-length"];
let newHeader = {
"user-agent": header["user-agent"],
"host": header["host"],
"connection": 'keep-alive'
};
console.log(newHeader)
request({
method: method,
url: url,
body: body,
headers: newHeader
}, function(error, httpResponse, body) {
if (error) {
console.error(url + " : " + error);
} else if (httpResponse.statusCode !== 204) {
reject(body.message);
} else {
resolve(body);
}
});
});
}
Here is route:
router.post('/v3_6/autoevents', function *() {
// save to db
yield EventAuto.save(this.request.body);
let akkaEndConfig = {
url: "http://127.0.0.1:8080/v3_6/autoevents",
method: 'POST',
header: this.header,
body: this.request.body
};
// request to another server
yield requestPromise(akkaEndConfig.url, akkaEndConfig.method, akkaEndConfig.header, akkaEndConfig.body);
this.status = 204;
});
But when I wanna run this server,got this error:
xxx POST /api/v3_6/autoevents 500 195ms -
TypeError: Cannot read property 'name' of undefined
at Object.callee$1$0$ (/koa/src/lib/error-trace.js:10:11)
at tryCatch(/koa/node_modules/regenerator/runtime.js:61:40)
at GeneratorFunctionPrototype.invoke [as _invoke](/node_modules/regenerator/runtime.js:328:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as throw] (/node_modules/regenerator/runtime.js:94:21)
at onRejected (/node_modules/co/index.js:81:24)
at run (/node_modules/core-js/modules/es6.promise.js:104:47)
at /node_modules/core-js/modules/es6.promise.js:115:28
at flush (/node_modules/core-js/modules/$.microtask.js:19:5)
at doNTCallback0 (node.js:428:9)
at process._tickDomainCallback (node.js:398:13)
I just wanna requset form serverA's route to serverB. Is this method wrong?
If you want to send a request from serverA to serverB, I created a sample app on what you want to achieve.
'use strict';
var koa = require('koa');
var route = require('koa-route');
var request = require('request-promise');
var rawBody = require('raw-body');
var appA = koa();
var appB = koa();
appA.use(route.get('/v3_6/autoevents', function *(){
let response = yield request({
method: 'POST',
url: 'http://127.0.0.1:8081/v3_6/autoevents',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ param1: 'Hello World from A' })
});
this.body = response;
}));
appB.use(route.post('/v3_6/autoevents', function *(){
this.req.body = yield rawBody(this.req,{limit:'10kb', encoding:'utf8'});
this.req.body = JSON.parse(this.req.body);
this.body = 'Hello from World B; ' + this.req.body.param1;
}));
appA.listen(8080);
appB.listen(8081);
Server A( appA ) has an endpoint named /v3_6/autoevents using GET method, accessing it will send a POST request to Server B's( appB ) /v3_6/autoevents endpoint that in return will send a truncated value with A's request body and Hello World from B;.
The final output after you execute it on the browser using http://127.0.0.1:8080/v3_6/autoevents will be Hello World from B; Hello World from A
In my Project I send Multipart form-data from angular side to nodejs. the format of data i received is
{ name: 'customer.test.14',
email: 'test14#gmail.net',
website: 'www.google.com',
contact_name: 'Vijay',
contact_number: '+123456789022',
profile: 'Testing',
provider_category: 'exchange',
services_offered: 'Testing',
description: 'Test',
image:
[ { size: 1474,
type: 'image/png',
path: 'bc31dac580a7c2086f306fe0b9b5182d/',
basename: 'icon_dd_chart_grey.png' } ] }
I want to send data this to another api in nodejs. but api does not upload image.
here is my code
var request = require('request');
var api_url = global.common.base_url + 'vcard/1.0.0/visit_card/' + req.param('uuid') +'/';
console.log(req.body);
request({
url: api_url,
method: 'PUT',
headers: {
'Content-Type': 'multipart/form-data;',
'Authorization': 'Bearer '+req.cookies.apitoken
},
json: req.body,
}, function(error, response, body) {
if(response.statusCode == 200 && !error){
res.end(JSON.stringify(body));
}else{
res.send(response.statusCode, { error: body });
}
});
You can archive this using "Okhttp3". Please refer this video tutorial form reference and usage and documentation.
Eg: upload two bodies (json and a image) to a single endpoint at the same time:
const okhttp = require('okhttp');
var MimeBuilder = okhttp.MimeBuilder;
var Request = okhttp.Request;
var RequestBody = okhttp.RequestBody;
var RequestBuilder = okhttp.RequestBuilder;
var FormEncodingBuilder = okhttp.FormEncodingBuilder;
var MultiPartBuilder = okhttp.MultiPartBuilder;
let json = JSON.stringify({title:'test'});
var image = fs.readFileSync(path.resolve(__dirname, 'test.jpg'));
let mp_body = new MultiPartBuilder().addPart(RequestBody.create(json, 'Content-Type: application/json; charset=UTF-8'))
.addPart(RequestBody.create(image, new MimeBuilder().contentType('image/jpeg').contentTransferEncoding('binary').build()))
.type(MultiPartBuilder.FORMDATA).build();
new RequestBuilder().url('https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart')
.header('Authorization', 'Bearer OAUTH2_TOKEN_HERE')
.POST(mp_body).buildAndExecute().then(console.log).catch(console.error);
I am trying to use the Bing Search API to return a JSON string. I first tried using the following url as per Azure's explore website (https://datamarket.azure.com/dataset/explore/5BA839F1-12CE-4CCE-BF57-A49D98D29A44):
'https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27web%27&Query=%27NGI%20SPA%27&Market=%27en-US%27'
After, I found a SO thread Using the new Bing API (nodejs) which suggested I use a url of the form:
https://user:<YourDefaultAccountKey>#api.datamarket.azure.com/Bing/SearchWeb/Web?Query=%27leo%20fender%27&Market=%27en-US%27&$top=50&$format=JSON
Both of these return status 401 (Authentication Failure):
STATUS: 401
HEADERS: {"content-type":"application/json; charset=utf-8","server":"Microsoft-IIS/8.0","jsonerror":"true","x-powered-by":"ASP.NET","access-control-allow-origin":"*","access-control-allow-credentials":"false","access-control-allow-headers":"Authorization, DataServiceVersion, MaxDataServiceVersion","access-control-expose-headers":"DataServiceVersion, MaxDataServiceVersion","access-control-allow-methods":"GET, POST, OPTIONS","access-control-max-age":"604800","date":"Wed, 02 Jul 2014 17:23:29 GMT","content-length":"91"}
BODY: {"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}
I have also tried other various combinations of URLs to no avail. My code is below:
var url = require('url');
var http = require('http');
var serviceRootURL = 'https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27web%27&Query=%27NGI%20SPA%27&Market=%27en-US%27'
var params = 'hi';
var dataURL = url.parse(serviceRootURL);
var post_options = {
hostname: dataURL.hostname,
port: dataURL.port || 80,
path: dataURL.path,
method: 'GET',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Content-Length': params.length
}
};
var req = http.request(post_options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
Any idea why I am getting an authentication failure?
You can use this module that encapsulates the requests, so you can use it
like:
var Bing = require('node-bing-api')({ accKey: "your-account-key" });
Bing.web("leo fender", function(error, res, body){
console.log(body);
},
{
top: 50,
market: 'en-US'
});
It works with the Azure version. You only have to replace your account key.
Got it working with request...weird
var request = require('request');
var _ = require('underscore');
var searchURL = 'https://user:<TIPE YOUR KEE HEER>#api.datamarket.azure.com/Bing/SearchWeb/v1/Web?Query=%27xbox%27&$top=10&$format=JSON';
var http = request( searchURL, function(err, resp, body)
{
if ( err )
{
throw err;
}
var a = JSON.parse(body);
console.log(a.d.results);
});
you can use jsearch module. install ;
npm install jsearch
usage;
js.bing('queryStringYouWant',10,function(response){
console.log(response) // for Bing results
})