in Now Experience Custom Component - How to specify "Content-Type" header to 'createHttpEffect'?

David Hubbard
Tera Guru

I am trying to use the Attachment API to upload an attachment from a Custom Component.

 

If I specify an action using createHttpEffect like:

 

 

    'UPLOAD_ATTACHMENT': createHttpEffect("/api/now/attachment/file", {
        method: 'POST',
        batch: httpBatchMode,
        queryParams: ['file_name', 'table_name', 'table_sys_id' ],
        dataParam: 'requestData',
        successActionType: 'UPLOAD_ATTACHMENT_SUCCESS',
        errorActionType: 'UPLOAD_ATTACHMENT_ERROR',
        headers: { 'MyHeader': 'YES', 'Content-Type': '*/*' }        
    }),

 

 

The request fails with "Missing parameter: Content-Type"

The request headers sent show the "MyHeader" is getting added, but "Content-Type" is not set.  I have tried using "contentType" and "ContentType" instead - both of these get added verbatim, but "Content-Type" does not get set.

 

Edit Update:  

Looking at the `createHTTPEffect` API this may be a flawed approach - as the Attachment API requires the Full "Path to the binary file to attach" and this is not something that Browsers provide (according to this) so solving the "Content-Type" header may only lead to next issue.

Any ideas?

 

2 REPLIES 2

Ratnakar7
Mega Sage
Mega Sage

Hi @David Hubbard ,

 

To specify the "Content-Type" header using createHttpEffect, you can add a "headers" property to the configuration object and set the value of "Content-Type" to the desired value.

For example:

 

'UPLOAD_ATTACHMENT': createHttpEffect("/api/now/attachment/file", {
    method: 'POST',
    batch: httpBatchMode,
    queryParams: ['file_name', 'table_name', 'table_sys_id' ],
    dataParam: 'requestData',
    successActionType: 'UPLOAD_ATTACHMENT_SUCCESS',
    errorActionType: 'UPLOAD_ATTACHMENT_ERROR',
    headers: { 
        'MyHeader': 'YES', 
        'Content-Type': 'application/json' // or any other content type you need
    }        
}),

 

 

If my response was helpful in resolving the issue, please consider accepting it as a solution by clicking on the Accept solution button and giving it a thumbs up 👍. This will benefit others who may have a similar question in the future.

 

Thank you!

Ratnakar

 

Ratnakar7 hi

 

Thanks for replying - the issue seems to be that the attachment API allows a content type of "*/*", but 'createHttpEffect' didn't seem to want to apply this.  

 

I have actually gone a slightly different direction and have managed to get "/api/now/attachment/upload"  working in 'createHttpEffect' using a <form> which doesn't hit this issue - for that the Content-Type is '

multipart/form-data'