How to pass JSON array using JAVA component in Mule 4, as i am getting error if i pass JSON array - mulesoft

Cannot coerce Array { encoding: UTF-8, mediaType: application/java; charset=UTF-8, mimeType: application/java, raw: org.mule.weave.v2.model.structure.SeqArrayValue#38b1b5db, class: java.util.ArrayList } ([{I_PARTNER_ID: "CNLIGHT01" as String {class: "java.lang.String"},I_ACTION_CO...) to Object" evaluating expression: "payload

Related

Angular 1.x image upload with custom attributes

I am using Angular 1.x to provide a multiple file upload feature via a drag and drop interface. I want to seed one of the images as a default using a flag.
I am using a FormData object which will be passed to my backend to be saved. This is the structure of the data before I push the images to the object:
listing_images: Array(3)
0: File
default_image: 1
lastModified: 1521848990788
lastModifiedDate: Fri Mar 23 2018 23:49:50 GMT+0000 (Greenwich Mean Time) {}
name: "002.JPG"
preview: "data:image/jpeg;base64,/9j/4f8ERXhpZgAATU0AKgAAAAg"
size: 136276
type: "image/jpeg"
webkitRelativePath: ""
__proto__: File
1: File ....
The snippet shows one of the 3 images which are to be uploaded - the first of which has the default_image flag set to be true, so far so good.
I then use the append() function to add the images to my formData:
var dataObj = new FormData();
angular.forEach(listing_images, function(key, value) {
dataObj.set(listing_images[key], value);
});
If I loop the entries() of the dataObj I see:
listing_images[0], [object File]
listing_images[1], [object File]
listing_images[2], [object File]
So all looks good, then on my backend after submitting the dataObj if I debug the request data I see this format for each image:
'tmp_name' => '\tmp\php82C1.tmp',
'error' => (int) 0,
'name' => '002.JPG',
'type' => 'image/jpeg',
'size' => '136276',
As can be seen, the default_image flag has been lost. Is there a way to retain the custom values which i have pushed to the file object?
If you check the FormData specification you can see that FormData values can be either USVStringBlob or File/Blob. So, you cannot send whatever you want object (unless you convert it to some string and parse on the server-side in an appropriate way).
If you need to send default_image property you should send it as an additional parameter. Smth like this, I think:
var dataObj = new FormData();
angular.forEach(listing_images, function(key, value) {
if (value.default_image) {
dataObj.set('default_image', key);
}
dataObj.set(listing_images[key], value);
});

userdata validation failed: typeofatrocity: Cast to Array failed for value "[object Object]" at path "typeofatrocity"

I am new to mongoose and MongoDB and I am trying to send data from my front end to the server and save it. But if I use my userdata Model as
typeofatrocity:{
type: String
}
The data is being stored as
"typeofatrocity": [
"[object Object],[object Object],[object Object]"
],
The console log of data I am sending data
This data was generated using react-select when I am selecting multiple options.
But if I change the userdata model to
var TypeofAtrocity = new mongoose.Schema([{
value: String,
label: String
}]);
typeofatrocity: {
type: [TypeofAtrocity]
}
I get the error that
"userdata validation failed: typeofatrocity: Cast to Array failed for value "[object Object]" at path "typeofatrocity"
I am sending this data as formdata using fromdata.append('typeofatrocity', typeofatrocity)
then passing the formdata to the backend using axios and this formdata.

How to fix the "Invalid JSON payload received. Unknown name" error

I try to get a proper response from the Google Safe Browsing API v4. Although I get the error "Invalid JSON payload received. Unknown name".
I have used a payload based on the payload example mentioned at https://developers.google.com/safe-browsing/v4/lookup-api
I have problems with the payload. I think it should be a string, not a real dict. When I use a dict I get the error: TypeError: has type , but expected one of: str, unicode
The code I used is:
result = urlfetch.fetch(url, method=urlfetch.POST, payload=payload)
The url is (with [api-key] is of course my api-key):
https://safebrowsing.googleapis.com/v4/threatMatches:find?key=[api-key]
The payload is the following string (not a python dict):
{
"client": {
"clientId": "myproject",
"clientVersion": "42" },
"threatInfo": {
"threatTypes": ["MALWARE", "SOCIAL_ENGINEERING"],
"platformTypes": ["WINDOWS"],
"threatEntryTypes": ["URL"],
"threatEntries": [ {"url":"http://www.example.com/"} ] }
}
As output I expected some JSON which indicates that this url is safe. However I get the following result:
{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"{\n \"client\": {\n \"clientId\": \"myproject\",\n \"clientVersion\": \"42\"\n },\n \"threatInfo\": {\n \"threatTypes\": [\"MALWARE\", \"SOCIAL_ENGINEERING\"],\n \"platformTypes\": [\"WINDOWS\"],\n \"threatEntryTypes\": [\"URL\"],\n \"threatEntries\": [\n {\"url\":\"http://www.example.com/\"}\n ]\n }\n }\": Cannot bind query parameter. Field '{\n \"client\": {\n \"clientId\": \"myproject\",\n \"clientVersion\": \"42\"\n },\n \"threatInfo\": {\n \"threatTypes\": [\"MALWARE\", \"SOCIAL_ENGINEERING\"],\n \"platformTypes\": [\"WINDOWS\"],\n \"threatEntryTypes\": [\"URL\"],\n \"threatEntries\": [\n {\"url\":\"http://www' could not be found in request message.",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"description": [similar as the message above]
}
]
}
]
}
}
Any ideas, what is wrong?
Thanks
The urlfetch should contain the HEADER Content-Type: application/json
result = urlfetch.fetch(url, method=urlfetch.POST, payload=payload, headers={'Content-Type': 'application/json'})

fetchMock: uncaught typeError

I have mocked my API-Respones with fetchMock (v.5.13.1). I have been working with it for a quite long time now and I didn't see this kind of behaviour yet.
I mocked two GET responses that are very similar.
fetchMock.get('glob:*/shippings/',"results":[
{"id": "1234", "status": "RELEASED", "foo": "bar"},
{"id": "5678", "status": "CREATED", "foo": "bar"},)
fetchMock.get('glob:*/shipping/myId1234',
{"id": "1234", "status": "RELEASED", "foo": "bar"})
Now, the first one works properly, but the second get returns me this error message:
fetch-mock.js:187 Uncaught TypeError: Invalid status RELEASED passed
on response object. To respond with a JSON object that has status as a
property assign the object to body e.g. {"body": {"status:
"registered"}}
I have an assumption, that I cant mock some response that contains a status, because thats in a way a reserved attribute for status codes, but I am not quite sure and I cant find any similar errors online.
For the second request to fetchMock, it assumes status to be one of the standard codes supplied as an integer. According to the docs, the config supplied to fetchMock expects the following params
If an object only contains properties from among those listed below it
is used to configure a Response to return
body: String | Object
Set the Response body. See the non-config Object section of the docs
below for behaviour when passed an Object
Server responded ok { token: 'abcdef' }
status: Integer
Set the Response status
200, 404, 503
headers: Object
Set the Response headers
{'Content-Type': 'text/html'}
redirectUrl: String
The url from which the Response should claim to originate from (to
imitate followed directs). Will also set redirected: true on the
response
throws: Error
Force fetch to return a Promise rejected with the value of throws
new TypeError('Failed to fetch')
However for a custom status attribute you can respond with a body
fetchMock.get('glob:*/shipping/myId1234', {
body: {"id": "1234", "status": "RELEASED", "foo": "bar"}
})

Swagger 2.0 : Array in header of a response

Anyone know why this syntax is not supported ?
The pagination-links send an error and I can't find a way to declare them as an array :
responses:
200:
description: An array of screens
schema:
type: array
items:
$ref: '#/definitions/Screens'
headers:
content-range:
type: string
format: '[min]-[max]/[Total]'
description: 'Range of the element returned by the api like 1-7/1000'
pagination-links:
type: array
items:
$ref: '#/definitions/Link'
Your help would be really appreciated,
Thank you very much for your help

Resources