Reputation: 11
I am using Paw Rest client that is supposed to generate the Javascript (jQuery) code for the HTTP PUT request I am using to send File (Image) in the body. I have my Header as
"Content-Type:image/jpeg", Body as "File"
In the generated Javascript code, the image content is missing!! See picture. Can anyone provide me the code so that I am able to send HTTP PUT request using html/javascript.
Upvotes: 1
Views: 104
Reputation: 3481
Paw tries to generate the code at best, and doesn't handle all edge cases. Sadly, files/image sending isn't implemented in the code generation for JavaScript... but here's a way to send a file content:
var fileContent = null; // file content comes here
// My API (PUT http://198.11.232.156/oslc/so/triWorkTaskRS/127368846/spi:cstImageIM)
jQuery.ajax({
url: "http://198.11.232.156/oslc/so/triWorkTaskRS/127368846/spi:cstImageIM",
type: "PUT",
headers: {
"Cookie": "JSESSIONID=...",
"Authorization": "Basic ...",
"Content-Type": "image/jpeg",
},
processData: false,
data: fileContent,
})
.done(function(data, textStatus, jqXHR) {
console.log("HTTP Request Succeeded: " + jqXHR.status);
console.log(data);
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.log("HTTP Request Failed");
})
.always(function() {
/* ... */
});
Now to retrieve a file's content in a web browser, you probably want to use the HTML5 FileReader
. Here's an interesting answer for it: https://stackoverflow.com/a/29176118/1698645
Upvotes: 0