Create Incident with attachment using POSTMAN

tabrez ahmed
Tera Contributor

Hi Everyone,

 

I Simply want to create an incident on my PDI with attachment using POSTMAN

I created Scripted RESR API and created POST resource  

But I'm getting error "

"error": {
        "detail": null,
        "message": "Invalid content-type. Supported request media types for this service are: [application/json, application/xml, text/xml]"
 
 
please let me know if anybody having similar experience of this scenario 
 
thanks
 
 
Scripted REST API: POST
 
 
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    try {
        // Content-Type check karo
        var contentType = request.contentType || '';
        if (contentType.indexOf('multipart/form-data') === -1) {
            response.setStatus(415);
            response.setBody({
                status: "error",
                message: "Invalid Content-Type. Expected multipart/form-data."
            });
            return;
        }

        // Form-data se short_description extract karo
        var formData = request.body.dataString ? JSON.parse(request.body.dataString) : {};
        var shortDescription = formData.short_description || "Default Incident Description";

        // Incident create karo
        var grIncident = new GlideRecord('incident');
        grIncident.initialize();
        grIncident.short_description = shortDescription;
        var incidentSysId = grIncident.insert();

        // Agar incident create hua
        if (incidentSysId) {
            // Attachment handle karo
            var attachments = request.attachments;
            if (attachments && attachments.length > 0) {
                var attachment = new GlideSysAttachment();
                var file = attachments[0]; // Pehla attachment lo
                attachment.write(
                    file.getContentStream(),
                    'incident',
                    incidentSysId,
                    file.fileName,
                    file.contentType
                );
            }

            // Success response bhejo
            response.setStatus(201);
            response.setBody({
                status: "success",
                incident_number: grIncident.number,
                sys_id: incidentSysId,
                message: "Incident created with attachment"
            });
        } else {
            response.setStatus(500);
            response.setBody({
                status: "error",
                message: "Failed to create incident"
            });
        }
    } catch (e) {
        // Error handle karo
        response.setStatus(500);
        response.setBody({
            status: "error",
            message: "Error: " + e.message
        });
    }
})(request, response);


 
 
Request Body:
tabrezahmed_0-1748003334656.png

Header used:

tabrezahmed_1-1748003383869.png

Error getting:

tabrezahmed_2-1748003426110.png

 

4 REPLIES 4

tabrez ahmed
Tera Contributor

@Ankur Bawiskar   could you please help?

tabrez ahmed
Tera Contributor

@tabrez ahmed 

check this link on how to use Scripted REST API where I shared solution, please enhance to accept other incident fields and create. this example talks about adding file to INC, but you can create and then attach

Sending attachments using custom scripted web service 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@tabrez ahmed 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader