1

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.

enter image description here

Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41
Puri
  • 11
  • 2
  • Welcome to Stack Overflow. To help you, I have added the image inline for you. However, this is not satisfactory. It would be preferable if you cut and pasted the text here. – Rohit Gupta Oct 20 '15 at 01:43

1 Answers1

0

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

Community
  • 1
  • 1
Micha Mazaheri
  • 3,481
  • 1
  • 21
  • 26