How to change content-type of unirest attachment

32 Views Asked by At

This is my first time using this library, and so far so good. I have successfully accessed an attachment stored on a server that needs to be migrated and migrated it to it's new home. This being said, it is being put in the record I am attaching it to with a content-type of octect-stream when it needs to be pdf. I have tried setting the headers, passing through type and the URL of the attachment as an array in the .attach.

Here is the code that is working with the wrong content-type:

` function update_ticket(args){
var domain = kiskadomain;
var api_key = kiskaApiEncode;

var url = `${domain}/api/v2/tickets/${args.id}`;        


unirest.put(url)
.headers({  
Authorization : "Basic " + api_key
})
.attach('attachments[]', args.url)
.end(function (response) {
console.log(response.body);
if (response.status === 200) {  
console.log({"body": response.body.ticket.attachments, "status": response.status});
}else{
console.log({"body": response.body, "status": response.status});
}
});
}`

Here is what I currently have that is throwing a 400:

`  function update_ticket(args){
var domain = kiskadomain;
var api_key = kiskaApiEncode;

var url = `${domain}/api/v2/tickets/${args.id}`;        

unirest.put(url)
.headers({  
Authorization : "Basic " + api_key
})
.attach('attachments', args.arr)
.end(function (response) {
console.log(response.body);
if (response.status === 200) {  
console.log({"body": response.body.ticket.attachments, "status": response.status});
}else{
console.log({"body": response.body, "status": response.status});
}
});
}

//args looks like args = { size: ticattach.attachments[0].size, id: 183, arr : [ ticattach.attachments[0].content_type, ticattach.attachments[0].attachment_url ] }`

This is throwing an error of "Invalid value 'undefined' for header 'Content-Length'"

I have tried setting content type in the header, passing it as an array and making a call to the attachment endpoint of the new product to change the content-type, but have had no luck.

0

There are 0 best solutions below