ALERT! The community will be read-only starting on April 19, 8am Pacific as the migration begins. Read more for important details.
ALERT! The community will be read-only starting on April 19, 8am Pacific as the migration begins.Read more for important details.
Absent Member.. Absent Member..
Absent Member..
8452 views

RESTful API - Attachments for Incident

Hello Experts

 

I am quite new to RESTful. We have configured REStful for incidents, service requests and problem. I would like to know how I can add attachments for the same.

 

The current setup is 

 

1. There is an User portal from there incidents or service requests are created.

2. Users may want to attach these tickets which will be pushed to SM

3. The attachments should be available in Service Manager side as well.

 

Any help would be much appreciated.

 

Abhinav

 

Kindly assign kudos if the post is helpful!
Tags (1)
0 Likes
15 Replies
Absent Member.. Absent Member..
Absent Member..

Hi

Not sure if this will help or not but there is a discussion

 

http://h30499.www3.hp.com/t5/HP-Service-Manager-Service/Attachment-and-Webservice/td-p/5354705#.UyL0yoXLKjY

"HP Support
If you find that this post or any post resolves your issue, please make sure to mark it as an accepted solution."
0 Likes
Commodore
Commodore

Hi,

 

yeah it's a good question - i spent some hours and found a solution.

 

I have tested with HPSM 9.33.0035 (WIN).

To simulate your external user portal, i have used soapUI in version 5.0.0.

 

My latest configuration is the following:

 

Method: POST

Endpoint: http://127.0.0.1:13080

Resource: /SM/9/rest/interactions/<your-interaction-id>/attachments

 

As Post Body, i have the following JSON Code (but i think, the post body is not required)

 

{
   "attachments" : [
   {
      "attachment" : {
         "name" : "soapui.log"
      }
   }]
}

 

 

I have attached an attachment in soapUI, named "soapui.log".

The Attachment is cached in my example.

The Content-Type of the attachment is "application/octet-stream".

 

Now the important part 🙂

 

You have to set a Header key named "Content-Dispostion".

The Value of this header is "attachment; filename=soapui.log"

 

The second Header key is "Content-Type".

The value of this header is "application/octet-stream" (without this, your attachment gets "application/json" as content-type.

 

Now, if you send your request to the HPSM, you should get the following response:

 

{
"Messages": [],
"ReturnCode": 0,
"attachment": {
"href": "cid:53467b610011401020594248",
"len": 85,
"name": "soapui.log",
"type": "application/octet-stream",
"xmime:contentType": "application/octet-stream"
}
}

 

 

Without the header (Content-Disposition) you will get something like this

 

{
"Messages": ["Attachment name is required."],
"ReturnCode": 71
}

 

 

I have attached some screenshots, which explains it in pictures 😉

 

 

Hope this helps.

 

 

Greets!

Cadet 1st Class
Cadet 1st Class

Hi,

I am trying to integrate SM - SNOW. Where i have written scripts for Open and update activities. Now i have started to code for attachments. Following is the code, i have used.

function sysattach(number,sysid)
{
print("Sending attachment");
var ahead = new Header("Accept","application/json, text/plain, image/jpeg, */*");
var cdhead = new Header("Content-Dispostion", "attachment; filename=tbl.filename");
var head=new Header("Content-type","application/octet-stream", "multipart/form-data, application/json, image/jpeg, text/plain, */*");
var req_header = new Header("Authorization", 'Basic YWRtaW46TmFnYXJqdW4kMQ==');
var header = new Array();
header.push(ahead);header.push(cdhead);header.push(req_header);header.push(head);

var f = new SCFile("probsummary")
var fqry = "number=\""+number+"\"";
var frc = f.doSelect(fqry);
var attachment = f.getAttachments();
for(var attachs in attachment)
{
print("Attachment Name:"+attachment[attachs].name);
print("Attachment value:"+attachment[attachs].stringValue);
print("Attachment calue length:"+attachment[attachs].stringValue.length);
print("Attachment type:"+attachment[attachs].type);
print("Attachment href:"+attachment[attachs].href);
var attchobj = f.getAttachment( attachment[ attachs ].href );
}
var attchfo = attchobj.value;
var encoded = base64Encode(attchfo);

var post_url="https://abcdef.service-now.com/api/now/attachment/file?table_name=incident&table_sys_id="+sysid+"&file_name="+attachment[attachs].name;
var body="";
body+="{";
//body+="\"payload\":\""+encoded+"\"";
body+="\"payload\":\""+attchfo+"\"";
body+="}";
//print("attachment body:"+body);
//print("inside sysattachment table");
try
{
var rcsys = doHTTPRequest("POST", post_url, header, body);
print(rcsys);
//print(ressys);
}
catch(e)
{
print(e)
}
return rcsys ;
}

The attachment is getting attached to IM in snow. But the content of the attachment is wrong. The content of the attachment is what i write in the body. not the actual attachment that is in hpsm

body+="{";
body+="\"payload\":\""+attchfo+"\"";
body+="}";

Can someone help me, where i am doing wrong.

Regards,

Nagarjun S

 

0 Likes

Bravo, well done.  it worked Thank you,

0 Likes
Cadet 3rd Class
Cadet 3rd Class

Hi,

 

Did you find the solution to this problem?

0 Likes
Lieutenant Commander Lieutenant Commander
Lieutenant Commander

Hi, I am trying to create an incident through REST API, The incident is getting created with the request body that am passing, but attachment values are getting ignored and not getting inserted during the creation of the incident. Could you please assist as how to pass attachment in the json request body.

0 Likes
Ensign Ensign
Ensign

I realise that this topic is a bit old but, I had figured out 95% of this through many hours of searching. However even with Content-Dispostion attachment; filename=test_2.txt for instance, i am still getting error 400 Bad Request, response "Attachment name is required"

Is there anything special that needs to be done in sm9 to allow attachments via API?

The timeline response (using insomnia to perform tests) shows the contents of the text file, followed by *We are completely uploaded fine

But then has the 400 bad request back.

0 Likes
Ensign Ensign
Ensign

EDIT: Turns out the user i was testing with was not created 😞
Now that im actually using a user that exists its all good.
0 Likes
Ensign Ensign
Ensign

Hi All,

Above explanation is very well described in detail. Unfortunately after doing exact the same steps I`m still not able to upload an attachment (in Postman or SoapUI) because of the message. Attachment name is required. 

I am using sm v9.64 and test the situation with Falcon. Also attachments are enabled on the extacces record for REST. 

What do I wrong, please help (See also the attached screenshot)

0 Likes
Micro Focus Expert
Micro Focus Expert

Your screen looks the same as MT03. Since the error message is complaining about the attachment name, you could test by renaming it to soapui.log or shorten it or remove REST in case REST is a reserved word. Those are the only suggestion that I can think of.

0 Likes
Ensign Ensign
Ensign

I was having this issue until i realised the issue is a typo above.

"Content-Dispostion", "attachment; filename=tbl.filename"

SHOULD BE

"Content-Disposition", "attachment; filename=tbl.filename"

Disposition is missing the letter "i" in it.

The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.