From client side I am sending object as
{
name: xyz,
data : [
{ k1: v1, k2: v2 },
{ k1: v3, k2: v4 }
]
}
But on server side I am getting it as
{
name: xyz,
data : {
k1: [v1, v3], k2: [v2, v4]
}
}
I am uploading image along with it. Content-type is multipart/form-data
On serverside using body-parser
This looks issue due to ng-upload
{
url: 'url',
method: 'POST',
headers: {'Content-Type': 'multipart/form-data'},
arrayKey: '',
data: {file : $files, data: $data}
}
arrayKey field needed to upload multiple files. Removing it leads to error "Unexpected field" due to issue in ng-upload
But adding above fields causes the issue mention above. Array elements in JSON objects are getting merged.
If field arrayKey is removed, then correct JSON object is received at server side
Related
Lately I have found mailchimp-api-v3 to be quite useful for managing our .1k list. Currently, I use the following to (1) create new tags, and (2) add the tag to contacts:
const MC = require('mailchimp-api-v3');
const mailchimp = new MC('<apiKey>');
mailchimp.batch([{
method: 'POST',
path: '/lists/<list_id>/segments',
body: {
name: '<tag1>',
static_segment: [<contact_list1>]
}
}, {
method: 'POST',
path: '/lists/<list_id>/segments',
body: {
name: '<tag2>',
static_segment: [<contact_list2>]
}
}])
.then(results => {
console.log( results );
})
.catch(errs => {
console.log( errs );
});
Sometimes there's need to add an existing tag to contacts. Whenever I try to use the above code, as expected, I get tag already exists error and contacts are not tagged with this existing tag.
How do I get a list of all existing tags? And how would one add an existing tag to contacts?
I finally figured out how to do that. The just updated documentation is much easier to navigate that before. It's now evident that there are two ways of adding a tag/segment to (a) member(s): you can add a single tag to a member or you can add a single tag to many members:
Bulk add with members_to_add: array
{
method: 'POST',
path: '/lists/{list_id}/segments/{segment_id}',
body: {members_to_add: [<email-addresses>]}
}
This is indeed what I needed and I have now been using it for a while. Just today I found out the other method:
Single add with email_address: string
{
method: 'POST',
path: '/lists/{list_id}/segments/{segment_id}/members',
email_address: {email_address}
}
I want to add a member to a distribution list. As apparently I can't do this using Microsoft Graph, I am trying to use Azure AD Graph API. I am using Node.js.
I am able to connect to Azure using the adal-node library. I get a token back, send requests, and get responses. (I can list groups, users, etc.).
I am following the Add Members documentation, but I am confused.
In the URL, is object_id the id of the Group to witch I want to add the member?
For myorganization, I am using the tennant_id.
Where do I specify the user data? Should I pass that in the POST? If so, whats' the format?
What is the $links in the URL?
Currently, I am doing this:
request.post(
"https://graph.windows.net/TENNANT_ID_HERE/groups/GROUP_ID_HERE/$links/members?api-version=1.6",
{
headers: {
Authorization: "Bearer " + TOKEN_HERE,
"Content-Type": "application/json"
},
form: { key: "value" } //should I put my user object here?
},
function(err, res, body) {
if (err) {
console.log("err: " + err);
} else {
console.log("res: " + JSON.stringify(res, null, 3));
}
}
);
I get the following error:
{
"odata.error": {
"code": "Request_BadRequest",
"message": {
"lang": "en",
"value": "A supported MIME type could not be found that matches the
content type of the response. None of the supported type(s) 'application/xml, text/xml,
application/json;odata=minimalmetadata;streaming=true, application/json;odata=minimalmetadata;
streaming=false, application/json;odata=minimalmetadata,
application/json;odata=fullmetadata;streaming=true,
application/json;odata=fullmetadata;streaming=false,
application/json;odata=fullmetadata,
application/json;odata=nometadata;streaming=true,
application/json;odata=nometadata;streaming=false,
application/json;odata=nometadata,
application/json;streaming=true,
application/json;streaming=false,
application/json;odata=verbose,
application/json'
matches the content type 'application/x-www-form-urlencoded'."
}
}
}
The short/most important answer is that neither Microsoft Graph or Azure AD Graph API supports distribution lists. From the documentation:
Important: You can only add members to security groups and mail-enabled security groups.
That said, this isn't technically why your call is failing here. Your code is reaching the point where it's failing due to the type of group you're working with. And while it won't help you with managing a distribution list, the following is what is actually happening.
The form: { key: "value" } option is telling request to send the payload as a URL Encoded Form (application/x-www-form-urlencoded). The API requires the payload be sent as JSON (application/json).
To send over JSON, you need to do two things:
Set the json option to true
Set the body value rather than the form value.
The proper code would look something like this:
request.post(
"https://graph.windows.net/{tenant-id}/groups/{group-id}/$links/members?api-version=1.6",
{
headers: {
Authorization: "Bearer " + TOKEN_HERE
},
json: true,
body: JSON.stringify({ url: "https://graph.windows.net/{tenant-id}/directoryObjects/{user-id}" })
},
function(err, res, body) {
if (err) {
console.log("err: " + err);
} else {
console.log("res: " + JSON.stringify(res, null, 3));
}
}
);
The $links parameter in the URI is telling the API that you're providing a link to another resource (in this case, a user record).
we could add member to a group with AD graph API.
post https://graph.windows.net/{tenantId}/groups/{groupobjectid}/$links/members?api-version=1.6
body
{
"url": "https://graph.windows.net/{tenantId}/directoryObjects/{userObjectId}"
}
Test it with Postman
I am trying to get a document file from phone storage when button is pressed and than upload it server. But i don't know which library to use and how to do it.
If you are willing to use a library you have both React-native-fetch-blob or axios.
If React-native-fetch-blob you can do it like this:
RNFetchBlob.fetch('POST', 'http://www.example.com/upload-form', {
Authorization : "Bearer access-token",
otherHeader : "foo",
'Content-Type' : 'multipart/form-data',
}, [
// element with property `filename` will be transformed into `file` in form data
{ name : 'avatar', filename : 'avatar.png', data: binaryDataInBase64},
// custom content type
{ name : 'avatar-png', filename : 'avatar-png.png', type:'image/png', data: binaryDataInBase64},
// part file from storage
{ name : 'avatar-foo', filename : 'avatar-foo.png', type:'image/foo', data: RNFetchBlob.wrap(path_to_a_file)},
// elements without property `filename` will be sent as plain text
{ name : 'name', data : 'user'},
{ name : 'info', data : JSON.stringify({
mail : 'example#example.com',
tel : '12345678'
})},
]).then((resp) => {
// ...
}).catch((err) => {
// ...
})
You will hae to manage to get the file path from libraries like RNFS or even RNFetch blob.
https://github.com/wkh237/react-native-fetch-blob
You can use axios too (https://github.com/mzabriskie/axios), but I don't use it so I can't help you any further.
The difference between both is the way they send the data. RNFB uses the fetch api and goes down to native to get the Base64 encoding, axios works over XMLHttpRequests, which is more likely to be used in internet browsers.
Hope it helps.
I've got the following code, but it doesn't seem to work:
var post_req = {
array: [
[ {
param1: 'something',
param2: 123
} ],
[ ],
[ ],
[ {
param2: 'something',
param4: 1234,
param1: 'hello'
} ]
]
};
var data_send = querystring.stringify(post_req);
var request = client.request('POST', '/', headers);
request.end(data_send);
and
if( req.method == 'POST' ) {
req.addListener('data', function(chunk)
{
POST = querystring.parse(chunk);
console.log(POST);
}
}
I end up with 5 sub-arrays, corresponding to the 5 parameters in the objects but with extra '][' characters in their names:
{ array:
[ { '][param1': 'something' }
, { '][param2': '123' }
, { '][param2': 'something' }
, { '][param4': '1234' }
, { '][param1': 'hello' }
]
}
There is a new node package that fixes this: "npm install qs".
https://github.com/ljharb/qs
"query string parser for node supporting nesting, as it was removed from 0.3.x, so this library provides the previous and commonly desired behaviour (and twice as fast)"
If anyone can tell me why it was removed from 0.3.x, I will give you an upvote for your comment. (I want my confidence in Node.js restored.)
To confirm my comment above, node's querystring.stringify function won't handle nested arrays (at the time of writing).
You can see the source of stringify at https://github.com/ry/node/blob/master/lib/querystring.js
Note that it handles one level of arrays but it doesn't recurse. When it finds an array it uses stringifyPrimitive to encode the array's values. You can see that stringifyPrimitive doesn't handle arrays, only number, boolean and string.
As I suggested in my comment, given that you're using a POST request a better idea would be to use JSON encoding for your complex data structure.
Or use https://github.com/visionmedia/node-querystring as suggested by #FriendlyDev
Don't use querystring.parse for recreating a JSON object sent as string. Use instead JSON.parse. And use JSON.stringify instead of querystring.stringify
querystring is useful when you send parameter encoded in the url or when you post a form. But there is no point in using it if you send just one JSON object with all your data.
In your scenario I would dismiss the querystring library and use JSON library, which is already included. It would be cleaner and faster.
http://www.json.org/js.html
I'm using jquery-1.4.2.min and jquery-ui-1.8.6.custom to get the autocomplete data on a jsp page, here is the code snippet:
$(document).ready(function() { $("input#airportfrom").autocomplete({minLength: 3,
source: function(request, response) { $.ajax({
url: "MLocationLookupSearchPopUpAuto.action?LANGUAGE=${param.LANGUAGE}&SITE=${param.SITE}&RESULT_FILTER=3",
dataType:"text/html",
data: { MATCH : $("#airportfrom").val() },
success: function(data) { response(data); } }); } }); });
The result returned is correct as I have used an alert(data); inside the success function and it gave correct result, but in the list, it is showing one character or one alphabet per line, hence if I want to get LONDON, it is displayed as:
l
o
n
d
o
n
Any idea why this happening ? Whether we have to give data as json only because here I'm getting the data from a jsp.
Try to split the response data into lines using "\n"
$("#tags").autocomplete({
source: function(request,response) {
$.ajax({
dataType: "text",
url: 'yourscript.php?extraParam=foo',
data: { term: request.term },
success: function(data) {
var lines = data.split("\n");
response(lines);
}
})}
});
I had the same problem and it was down to serializing the object twice (by mistake) on the server side. The JSON data returned to the client was being de-serialized to a string rather than an array.