How can I use Microsoft.Translator.TranslateArray method in Node.js to send and receive data in JSON format as XML not allow\u001b etc. unicode. I am sending JSON request but getting this error:
"ArgumentOutOfRangeException: 'to' must be a valid language\u000d\u000aParameter >name: to : ID=5639.V2_Json.TranslateArray.12E99F9A"
You can use REST API to translate array of texts
POST http://api.microsofttranslator.com/V2/Http.svc/TranslateArray
Check the reference for details
you can use request module to hit this API.
Example:
var request = require('request');
request({
method: 'POST',
url: 'http://api.microsofttranslator.com/V2/Http.svc/TranslateArray',
headers: {
Authorization: 'Bearer *********' //put accesstoken
},
json: data //put your JSON here
}, function(err, res, body) {});
Related
I'm trying to post formdata with help of fetch api. Code :
var formData = new FormData();
formData.append("nickname", "johxns");
formData.append("password", "john_password");
formData.append("email", "john#server.com");
fetch("http://example/page", {
body: formData,
method: "post"
})
But in backend I'm recieving empty req body { }.
An object can be posted but why can't I post form data.
How to get this form data in backend.
You need to tell that you are sending form data not just body.
Add
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
I am trying to create a SharePoint list view through the SharePoint REST API, with a defined set of columns to be part of the view. The endpoint i am using is below:
POSTMAN API Request:
HTTP METHOD: POST
URL: https://tenantname.sharepoint.com/sites/SPSite/_api/web/lists/getbytitle('ListName')/views
Headers:
'Accept' - 'application/json;odata=verbose'
'Content-Type' - 'application/json;odata=verbose'
Body (JSON):
{
"__metadata":{
"type":"SP.View"
},
"Title":"TestView",
"ViewFields":["Title","Name"]
}
I get a JSON error, since this payload does not seem to be right. Need help in understanding how to create a view with specific fields through the SharePoint REST API.
Thanks,
Yesh
When creating view, it's not supported to add viewFields, this needs to be done after creating list view.
So please create the view like this firstly:
var viewQuery = "<OrderBy><FieldRef Name=\"ID\" /></OrderBy>";
$.ajax
({
// _spPageContextInfo.webAbsoluteUrl - will give absolute URL of the site where you are running the code.
// You can replace this with other site URL where you want to apply the function
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('MyList')/views",
type: "POST",
data: "{'__metadata':{'type': 'SP.View'},'ViewType': 'HTML','Title':'New View Created From REST','PersonalView':false,'ViewQuery':'" + viewQuery + "'}",
headers:
{
// Accept header: Specifies the format for response data from the server.
"Accept": "application/json;odata=verbose",
//Content-Type header: Specifies the format of the data that the client is sending to the server
"Content-Type": "application/json;odata=verbose",
// X-RequestDigest header: When you send a POST request, it must include the form digest value in X-RequestDigest header
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data, status, xhr) {
alert(data.d.Id);
},
error: function (xhr, status, error) {
console.log("Failed");
}
});
Then set Viewfield for the new created List View like this:
$.ajax
({
// _spPageContextInfo.webAbsoluteUrl - will give absolute URL of the site where you are running the code.
// You can replace this with other site URL where you want to apply the function
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('MyList')/Views(guid'58cfaaa2-107c-4a94-8490-38d1df195e5b')/ViewFields/addviewfield('Created')",
type: "POST",
headers:
{
// Accept header: Specifies the format for response data from the server.
"Accept": "application/json;odata=verbose",
//Content-Type header: Specifies the format of the data that the client is sending to the server
"Content-Type": "application/json;odata=verbose",
// X-RequestDigest header: When you send a POST request, it must include the form digest value in X-RequestDigest header
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data, status, xhr) {
console.log("Success");
},
error: function (xhr, status, error) {
console.log("Failed");
}
});
So the above sample is adding "Created" field into viewFields and View Guid is alert in first rerquest, use it in second request.
I have a python flask server running and its api is exposed. It is being used for converting json to excel file. I need to pass json through node code to that api and get back the response which would be a blob and save as a excel file locally. My first step is to display the blob object in console but I am getting a lot of data in console which is irrelevant. Here is my nodejs code
const request = require('request');
jsonobj={...} //a big json not displaying here
request(
{
method:'post',
url:'http://127.0.0.1:8095/excel/download',
form: jsonobj,
headers: {
"content-type": "application/json"
},
json: true,
}, function (error, response, body) {
//Print the Response
console.log(body);
});
The body is displaying a lot of data, I only need to display the excel blob
Ok, I changed form to body in request and it resolved. Here is the code but I still need to convert blob object and save as an excel file locally. -
method:'post',
url:'http://127.0.0.1:8095/excel/download',
body: jsonobj, //changed form to body here
headers: {
"content-type": "application/json"
I have to implement the following workflow:
Make request no.1, get a JSON response. Manipulate this JSON object so that it can be used as payload in the second request. Make second request.
The first request is successfully implemented. I get the correct JSON object. The manipulation part is also done and tested.
I managed to create a correct postman collection for the second request. It uses the method POST, has several headers, Content-type is multipart/form-data and the payload from request no.1 is sent as Raw (in the body, options none, form-data etc ... I choose raw).
I am trying to implement this second request but get an error no matter what I try to tweak. The following is my code:
const manipulatedObject = await this._flattenPayload(payload);
const Request = require(request-promise);
const options = {
method: 'POST',
uri: 'https://second_request_url',
formData: {
file: {
value: manipulatedObject,
},
},
headers: {
Accept: 'application/json, text/plain, */*',
Connection: 'keep-alive',
'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryxxxxxxxxxxxxxxxx', // this is generated dynamically and matches the value in the manipulatedObject
cookie: this.cachedCookie,
'X-XSRF-TOKEN': this.cachedToken,
},
};
Request(options)
.then((body) => {
return body;
})
.catch((error) => {
return error;
});
* The parameter payload is the response from the first request.
The error I get is this:
message=source.on is not a function, stack=TypeError: source.on is not
a function
The variable manipulatedObject is a string. Currently I am copying it's value from the successful postman request to avoid errors from the manipulation process. The random token in the Content-Type header matches the ones in the manipulatedObject string. So the data are identical to the ones I use in the postman request.
I suspect the error is in the way I send the data, but I am having trouble figuring it out. I have read about this error and it seems to be generated when an object is passed to formData, but I am sending a string so I don't understand why it happens.
The values of formData accepts only three types of elements viz. string, Buffer and Stream. Refer to:request/issues/2366
U may change formData as follows:
formData: {
file: manipulatedObject,
},
or
formData: {
file: {
value: manipulatedObject,
options: {},
},
},
I am trying to upload an image with the box API and the request module. I tried the provided curl example without any problems.
I have a request all setup like this
var request = require("request");
var fs = require("fs");
var path = require("path");
request({
url: "https://api.box.com/2.0/files/content",
method: "POST",
form: {
filename: fs.createReadStream(path.join(__dirname, "midguts.jpg")),
folder_id: "0"
},
headers: {
api_key: "<API_KEY>",
auth_token: "<AUTH_TOKEN>"
}
}, function (error, response, body) {
console.log(error);
console.log(body);
});
The problem arises when I get to the headers part. The box API call for a headers string of
"Authorization: BoxAuth api_key=API_KEY&auth_token=AUTH_TOKEN"
but I with the request module I can only send an object of key, value pairs. I also looked at the docs for nodes http.request and found it has the same issue.
So the question is, why does the API not follow the standard key pair format and how can I send a POST request that will work?
Authorization is the name of the HTTP Header (see also). This might work better:
headers: {
Authorization: "BoxAuth api_key=API_KEY&auth_token=AUTH_TOKEN"
}