worklight adapter using restful webservice - ibm-mobilefirst

I am using worklight adapter to implementing on restful web service. but i can not get any successful response.
This is my adapter:
function getTest(username,password) {
var path = "http://test.mybluemix.net";
var request = 'username='+username+'&'+'password='+password;
var input = {
method : 'get',
returnedContentType : 'plain',
path : path,
headers: {
"Host":"http://test.mybluemix.net"
},
body : {
contentType: 'text/xml; charset=UTF-8',
content: request.toString()
}
};
return WL.Server.invokeHttp(input);
}
this i got response for when i call the adapter..
response header is connection close and X-Backside-Transport is failed
normally i have to hit above the url its working fine http://test.mybluemix.net/?username=sssss&password=dffa
response : {"ID":"5","USERNAME":"sassad","PASSWORD":"adsa","ROLE":"abc","PHONENUMBER":"12345678"}

Several things look wrong to me.
The path variable should not point to the hostname. It's supposed to be the part afterwards:
In the adapter XML file you define the protocol, host and port values.
Then you provide the path, for example: http://myhost:8080/THE-PATH. In this case I don't think you need it since according to your working URL example there is no actual path - there are only the parameters, the request.
I'm not sure you need the host header. Try w/out it first...
Try this. Hopefully that'll work.
function getTest(username,password) {
var request = 'username='+username+'&'+'password='+password;
var input = {
method : 'get',
returnedContentType : 'plain',
path : path,
//headers: {
// "Host":"http://test.mybluemix.net"
// },
body : {
contentType: 'text/xml; charset=UTF-8',
content: request.toString()
}
};
return WL.Server.invokeHttp(input);
}

Related

Getting error while framing request URL using appscript

I am trying to frame request for API using appscript.
var url_string = "https://*.cognitiveservices.azure.com/vision/v3.2/describe"
let body = {
'"url"':'"https://www.khwaahish.com/wp-content/uploads/2022/01/khwaahish-white-bg-logo.jpg"'
};
const headers = {
'method' : 'POST',
'Host':'imagealttextcreation.cognitiveservices.azure.com',
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key':'###',
'payload': body
};
var response = UrlFetchApp.fetch(url_string,headers)
Logger.log(response)
I am getting invalid request error. But the same thing is working when i try manually(attached image for same).
Am I missing something while forming this request in the appscript?
When tried manually using the browser the functionality works. i want help in correcting the request using appscript.
From the official document, in your script, how about the following modification?
Modified script:
var url_string = "https://*.cognitiveservices.azure.com/vision/v3.2/describe";
let body = { url: "https://www.khwaahish.com/wp-content/uploads/2022/01/khwaahish-white-bg-logo.jpg" };
const options = {
headers: { "Ocp-Apim-Subscription-Key": "###" }, // Please set your value.
payload: JSON.stringify(body),
contentType: "application/json"
};
var response = UrlFetchApp.fetch(url_string, options);
Logger.log(response.getContentText())
Reference:
fetch(url, params)

React native post image to server Required request part 'file' is not present

There is no problem with sending photos from Postman.
header is ---> 'Content-Type':'application/x-www-form-urlencoded'
body is ----> form-data , {file : image..}
Sending headers to x-www-form-urlencoded or multi-part/form-data does not work.
(HTTP Status 400 – Bad Request)
Note that there is no image capacity limitation in the API.
Check out the screenshot for more postman.
I stay overnight for a few days. Please help me....
in my code
let localUri = this.state.image; // <--- is image uri.
let filename = localUri.split('/').pop();
let match = /\.(\w+)$/.exec(filename);
let type = match ? `image/${match[1]}` : `image`;
let formData = new FormData();
formData.append('photo', { file: localUri, name: filename, type: type });
return fetch(MY_SERVER, {
method: 'POST',
body: formData,
headers: {
'Content-Type':'application/x-www-form-urlencoded'
},
}).then((response) => response.text())
.then((responseData) => {
console.log(responseData);
console.log('file',formData)
})
.done();
in error messege
I don't think I can find the key called file.
Is this an API issue?
HTTP Status 400 – Bad Request
Required request part 'file' is not present
I think the error resides in this line of code:
formData.append('photo', { file: localUri, name: filename, type: type });
Instead try to append like the following:
formData.append('file', localUri);
formData.append('name', filename);
formData.append('type', type);

how to submit json file in sencha touch in http post multipart?

I want to submit json file form sencha touch to my tomcat server using http post multipart but i don't know how to do ?
can any one give me some idea or example.
Thanks
you can do this using jQuery.
var request = new FormData();
$.each(context.prototype.fileData, function(i, obj) { request.append(i, obj.value.files[0]); });
request.append('action', 'upload');
request.append('id', response.obj.id);
$.ajax({
type : 'POST',
url : context.controller,
data : request,
processData : false,
contentType : false,
success : function(r) {
console.log(r);
//if (errors != null) { } else context.close();
},
error : function(r) { alert('jQuery Error'); }
});

Sencha Touch: Using proxy and POST request and URL generation

I have been trying to do a post request using a proxy. I have tried the direct proxy, rest and ajax proxy, and haven't been able to find a working example for a POST request.
Is it possible? Because all the examples that I have seen seen to be using only GET.
Any working examples, or pointers in this direction?
Also, I couldn't figure what is the correct way to generate URLs for a proxy at run-time, for example, calling a function to return the URL.
It appears that this might not be possible:
http://www.sencha.com/forum/showthread.php?205557-Using-Ext.data.proxy.Ajax-via-a-POST-with-jsonData
If you look at the source code for Ext.data.proxy.Rest you'll see a config object for actionMethods. They're not documented, but you should be able to pass that as a config on your proxy to override it.
For example:
proxy: {
type: 'ajax',
url: 'path/to/foo',
actionMethods: {
create : 'POST',
read : 'POST',
update : 'PUT',
destroy: 'DELETE'
},
reader: {
type: 'json',
rootProperty: 'root',
totalProperty : 'totalCount'
}
}
Easiest example of POST request could be like this:
var obj = new Object();
obj.userId = username;
obj.password = password;
var data = Ext.JSON.encode(obj);
Ext.Ajax.request({
url : 'http://myservice/auth/login?_type=json', // url : this.getUrl(),
method : "POST",
headers: {
'Content-Type': 'application/json'
},
params : data,
useDefaultXhrHeader : false,
withCredentials: true,
success : function(response) {
Ext.Msg.alert("Success", "Welcome "+respObj.user.name);
},
failure : function(response) {
var respObj = Ext.JSON.decode(response.responseText);
Ext.Msg.alert("Error", respObj.status.statusMessage);
}
});
Please note here you can customize url as per your convenience.

worklight adapter invoke web service. error Cannot read property Body

Invoking the following adapter return Ecma Error: TypeError: Cannot read property \"Body\" from undefined.
I have read similar threads and had
-Dorg.xml.sax.driver = com.sun.org.apache.xerces.internal.parsers.SAXParser
to eclipse.ini
but didn't solve the issue.
function getStateDetails(idstate) {
var request='<?xml version="1.0" encoding="utf-8"?>'+
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
+'<soap:Body>'
+ '<test_demo><in0>{idstate}</in0></test_demo>'
+'</soap:Body>'
+'</soap:Envelope>';
var input = {
method : 'post',
returnedContentType : 'xml',
path : '/axis2/services/ws_demo/test_demo.wsdl',
body : {
content: request.toString(),
contentType: 'text/xml; charset=utf-8'
}
};
var result = WL.Server.invokeHttp(input);
return result.Envelope.Body;
}
finally it works fine with the help of soapui and adding headers in the request.
function getStateDetails(idstate) {
var request='<?xml version="1.0" encoding="UTF-8"?>'
+'<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">'
+'<Body><test_demo xmlns="http://www.ibm.com/informix/i4gl-soa/2010-11/ws_commandes">'
+'<in0>'+idstate+'</in0></test_demo>'
+'</Body></Envelope>';
WL.Logger.debug("SOAP Request " + request);
var input = {
method : 'post',
returnedContentType : 'xml',
headers: {SOAPAction: 'test_demo'},
path : '/axis2/services/ws_commandes',
body : {
content: request.toString(),
contentType: 'text/xml; charset=utf-8'
}
};
var result = WL.Server.invokeHttp(input);
return result.Envelope.Body.test_demo_response;
}
Looks like you're making a request to the WSDL, not service itself.