Reputation: 12406
I'm trying to do GET
that retrieves data from a URL and then a POST
to a RESTful api. The get call seems to work okay but the server sees nothing in the file
parameter. I have verified that the GET
call is return pdf stream data and that it is placing it in the FormData
arg.
Here is the complete call
function upload(key, url){
var file;
/* get data from url */
$.ajax({
url: url,
type: 'GET',
async: false,
cache: false,
dataType : 'text',
contentType : 'application/pdf',
success: function( data ) {
file = data;
},
error: function(xhr, status, error) {
console.log("error logging: "+ error);
}
});
/* send data to api */
var data = new FormData();
data.append("key", key);
data.append("file", file); //<-- this has pdf stream data
$.ajax({
url: ROOT_URL + "/api/account/upload",
type: 'POST',
cache: false,
async: false,
enctype: 'multipart/form-data',
processData: false,
contentType: false,
data: data,
success: function( data ) {
console.log("Uploaded!");
},
error: function(xhr, status, error) {
console.log("error logging: "+ error);
},
dataType: "json"
});
};
Here is the server side (grails + spring + jaxrs)
@POST
@Path('upload')
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces('application/json')
String upload(@Context HttpServletRequest request){
GrailsWebRequest w = WebUtils.retrieveGrailsWebRequest()
MultipartFile multipartFile = w.getRequest().getFile('file');
println("request:" + request)
println("multipartFile:" + multipartFile)
println("parameterMap:" + w.getRequest().getParameterMap() )
return ['okay':'uploaded'] as JSON
}
Which currently prints out:
request:org.grails.jaxrs.web.RequestWrapper@448593df
multipartFile:null
parameterMap:[file:[], key:[c07fc0974ebb4f3a8fc21e3d002152d4]]
Upvotes: 0
Views: 215
Reputation: 105
Looks like when the POST call is happening, the variable file doesn't have a value yet. Since you are wanting everything after GET call to occur after the server has returned data, you should move that code into a separate function that is called upon success.
Upvotes: 2