I searched here and there and ended up with no finding regarding putAsync method of promisified request by bluebird.
var request = require('request');
var Promise = require('bluebird');
Promise.promisifyAll(require("request"));
request.putAsync({
uri: buApiUrl,
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
name: BU,
workstations: formattedWorkStaions[BU]
})
}).spread(function (response, body) {
debugHelper.log(body);
}).catch(function (err) {
debugHelper.error(err);
});
Above is the code snippet that is in my program. And it does not send put request. While using postAsync, if will send post request successfully.
Can anyone help explain why?
I already found the part where is wrong in the putAsync code snippet. I should use json not body as the key for the payload. And the payload does not need to be stringified.
Here is the new code snippet proven to work.
var request = require('request');
var Promise = require('bluebird');
Promise.promisifyAll(require("request"));
request.putAsync({
uri: buApiUrl,
headers: {
'content-type': 'application/json'
},
json: {
name: BU,
workstations: formattedWorkStaions[BU]
}
}).spread(function (response, body) {
debugHelper.log(body);
}).catch(function (err) {
debugHelper.error(err);
});
This is quite tricky and result in my second question.
Why there is such a difference between post and put other than their method type?
Related
var request = require('request'); //node module for http post requests
exports.retreiveMessage = function (session){
request.post({
url: 'https://southcentralus.api.cognitive.microsoft.com/customvision/v1.0/Prediction/279ae65a-c1f8-4eb0-a4d8-03a3234bc023/image?iterationId=bcfb842f-df51-47e3-8ba4-c90209a16003',
json: true,
headers: {
'Content-Type': 'application/octet-stream',
'Prediction-Key': 'XXXXX'
},
body: session.message.attachments[0]
}, function(error, response, body){
if (error){
console.log(error);
}
console.log(validResponse(body));
session.send(validResponse(body));
});
}
function validResponse(body){
if (body && body.Predictions && body.Predictions[0].Tag){
return "This is " + body.Predictions[0].Tag
} else{
console.log('Oops, please try again! Something is wrong with custom vision.');
}
}
This is the block of code that I'm trying to use to post an image attachment to Custom Vision API, but I keep getting BadRequestImageFormat and I don't know what to do. Any help is appreciated.
I think it's due to the reference part of request body is not clear at the documentation, but we can from the c# code sample at https://learn.microsoft.com/en-us/azure/cognitive-services/custom-vision-service/use-prediction-api, that we need to post the image binary in body property.
And in Bot Framework, the session.message.attachments[0] is an object with the attachments info not content in buffer type:
which rised your issue.
Please try following code snippet:
const rp = require('request-promise');
rp.get(session.message.attachments[0].contentUrl).then(buffer=>{
return rp.post(
{
url:<url>,
headers: {
'Content-Type': 'application/octet-stream',
'Prediction-Key': '<key>'
},
body: buffer
}
)
}).then(res=>{
console.log(res);
session.send(res)
})
You need to remove json: true.
I try to use the node-soap module like this:
const soap = require('soap');
soap.createClient('some-wsdl-url', function(err, client) {
const args = {
'ValidateCustomerRequest': {
'memberNumber': 12345
}
};
client.ValidateCustomer(args, function(err, result) {
console.log(result);
});
});
Now I get an "invalid format" response from the webservice. To debug this I would very much like to see what the actual XML looks like that is sent to the webservice.
I already tried this one:
require('request').debug = true
... which was suggestes in another SO answer.
But the output is not that helpful:
[ERROR] REQUEST { uri:
Url { ... },
method: 'GET',
headers:
{ 'User-Agent': 'node-soap/0.18.0',
Accept: 'text/html,application/xhtml+xml,application/xml,text/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding': 'none',
'Accept-Charset': 'utf-8',
Connection: 'close',
Host: 'xxx' },
followAllRedirects: true,
body: null,
callback: [Function] }
I don't see my "ValidateCustomerRequest" in there and body is null. Also I'm wondering why method is GET, shouldn't that be POST?
So does this debug output looks normal and/or is there another way to see the created XML request?
I overlooked this bit of the documentation:
Client.lastRequest - the property that contains last full soap request for client logging
You can log this within the callback of the webservice call. So above code will look like this:
const soap = require('soap');
soap.createClient('some-wsdl-url', function(err, client) {
const args = {
'ValidateCustomerRequest': {
'memberNumber': 12345
}
};
client.ValidateCustomer(args, function(err, result) {
console.log(result);
console.log('last request: ', client.lastRequest) // <-- here
});
});
This will output the generated XML request.
I am trying to call an external rest API from node server by using request node module.
let request = require('request');
var options = {
method: 'POST',
url: 'https://somerestURI:3000',
qs: { msg: 'some|data|for|other|server' }
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
If I try to run the above code, query string value is being encoded to
some%7cdata%7cfor%7cother%7cserver
as a result I am not receiving correct response.
But if I fire the same request in POSTMAN. I am receiving the expected output(I think postman is not encoding query string).
So what I want is don't encode the query string value.
Any help would be greatly appreciated.
As answered here, you can disable encoding in qsStringifyOptions
var options = {
method: 'POST',
url: 'https://somerestURI:3000',
qs: { msg: 'some|data|for|other|server' },
qsStringifyOptions: {
encoding: false
}
};
You can use node-rest-client package. It allows connecting to any REST API and get results as javascript Object.
var HttpClient = require('node-rest-client').Client;
var httpClient = new HttpClient();
// GET Call
httpClient.get("http://remote.site/rest/xml/method", function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});)
or for POST Call
var args = {
data: { test: "hello" },
headers: { "Content-Type": "application/json" }
};
//POST Call
httpClient.post("http://remote.site/rest/xml/method", args, function (data, response) {
// parsed response body as js object
console.log(data);
// raw response
console.log(response);
});
Regarding to this question:
Transfer / pass cookies from one request to another in nodejs/protractor
I got another one. How could I view complete request (headers + body) which I am performing via nodejs?
Yes, you can ... You can access the complete request from the full response body - response.request
I have a generic full response structure illustrated below
IncomingMessage
ReadableState
headers(ResponseHeaders)
rawHeaders
request - //This is what you need
headers
body
body(Response Body)
You can access through code as shown below
var request = require("request");
var options = { method: 'POST',
url: 'http://1.1.1.1/login',
headers:
{ 'cache-control': 'no-cache',
'content-type': 'application/json' },
body: { email: 'junk#junk.com', password: 'junk#123' },
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
// This will give you complete request
console.log(response.request);
//This gives you the headers from Request
console.log(response.request.headers);
});
I'm trying to implement https://developers.podio.com/doc/items/add-new-item-22362 Podio API addItem call in a nodejs module. Here is the code:
var _makeRequest = function(type, url, params, cb) {
var headers = {};
if(_isAuthenticated) {
headers.Authorization = 'OAuth2 ' + _access_token ;
}
console.log(url,params);
_request({method: type, url: url, json: true, form: params, headers: headers},function (error, response, body) {
if(!error && response.statusCode == 200) {
cb.call(this,body);
} else {
console.log('Error occured while launching a request to Podio: ' + error + '; body: ' + JSON.stringify (body));
}
});
}
exports.addItem = function(app_id, field_values, cb) {
_makeRequest('POST', _baseUrl + "/item/app/" + app_id + '/',{fields: {'title': 'fgdsfgdsf'}},function(response) {
cb.call(this,response);
});
It returns the following error:
{"error_propagate":false,"error_parameters":{},"error_detail":null,"error_description":"No matching operation could be found. No body was given.","error":"not_found"}
Only "title" attribute is required in the app - I checked that in Podio GUI. I also tried to remove trailing slash from the url where I post to, then a similar error occurs, but with the URL not found message in the error description.
I'm going to setup a proxy to catch a raw request, but maybe someone just sees the error in the code?
Any help is appreciated.
Nevermind on this, I found a solution. The thing is that addItem call was my first "real"-API method implementation with JSON parameters in the body. The former calls were authentication and getApp which is GET and doesn't have any parameters.
The problem is that Podio supports POST key-value pairs for authentication, but doesn't support this for all the calls, and I was trying to utilize single _makeRequest() method for all the calls, both auth and real-API ones.
Looks like I need to implement one for auth and one for all API calls.
Anyway, if someone needs a working proof of concept for addItem call on node, here it is (assuming you've got an auth token beforehand)
_request({method: 'POST', url: "https://api.podio.com/item/app/" + app_id + '/', headers: headers, body: JSON.stringify({fields: {'title': 'gdfgdsfgds'}})},function(error, response, body) {
console.log(body);
});
You should set content-type to application/json
send the body as stringfied json.
const getHeaders = async () => {
const headers = {
Accept: 'application/json',
'Content-Type': 'application/json; charset=utf-8',
};
const token = "YOUR APP TOKEN HERE";
headers.Authorization = `Bearer ${token}`;
return headers;
}
const createItem = async (data) => {
const uri = `https://api.podio.com/item/app/${APP_ID}/`;
const payload = {
fields: {
[data.FIELD_ID]: [data.FIELD_VALUE],
},
};
const response = await fetch(uri, {
method: 'POST',
headers: await getHeaders(),
body: JSON.stringify(payload),
});
const newItem = await response.json();
return newItem;
}