问题
We have mobilefirst adapter its working properly in swagger tool but when we are trying to invoke from client side its giving error for unsupported media type-errorcode 415.Environment mobilefirst 8.0 with ibm cloud object storage .
server side java adapter code below like that,
-----------------------------------------
@POST
@Path("/uploadImageToObjectStorage")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@OAuthSecurity(enabled = false)
public String uploadImage(@QueryParam(value="imageName") String imageNameWithExtn, @QueryParam(value="imageMimeType") String imageMimeType, @FormParam(value="imageDataInBase64Str") String imageBase64String){
/// do to
}
cordova ionic 2 calling adapter or invoke adapter like that,
-------------------------------------------------------------
var resourceRequest = new WLResourceRequest("/adapters/Test/TestAdp/upload",WLResourceRequest.POST);
resourceRequest.send().then((response) => {
resourceRequest.setQueryParameter("imageName", 'store');
resourceRequest.setQueryParameter("imageMimeType",'image/png');
resourceRequest.setQueryParameter("imageDataInBase64Str",this.imageDataInBase64Str);
},
function(error){
console.log(error);
});
{"status":415,"statusText":"Unsupported Media Type","responseText":"","responseHeaders":{"connection":"Keep-Alive","content-language":"en-US","date":"Tue, 21 Nov 2017 14:09:31 GMT","transfer-encoding":"chunked","x-backside-transport":"FAIL FAIL","x-global-transaction-id":"4266505935","x-powered-by":"Servlet/3.1"},"errorMsg":"Unsupported Media Type","errorCode":"415"}
回答1:
You should set the Content-Type of your resourceRequest to application/ x-www-form-urlencoded
.
回答2:
Add this line in your code
resourceRequest.setHeader('Content-Type','application/x-www-form urlencoded');
回答3:
I was implementing the device token adapter using IBM Work light and i got same error :
unsupported media (45) type error
when i used this :
request?.setHeaderValue("application/json" as NSObject, forName: "Content-Type")
Resolved my issue , try this.
来源:https://stackoverflow.com/questions/47415586/mobilefirst-adapter-invocation-failed-with-unsupported-media-type-error