How can api payload as {data:{ ?!

dave_edgar
Mega Guru

This this is my, doctored, outbound BR script to post an incident ticket to a 3rd party vendor:

var request = new sn_ws.RESTMessageV2();

request.setEndpoint('https://company.com/endpointRestService/createOrUpdateTicket');

request.setHttpMethod('POST');

request.setRequestHeader('authCode','code here');

request.setRequestHeader('Accept','application/x-www-form-urlencoded');

request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

request.setRequestBody('{data: {"message":"SNow call to api","secondaryContactEmail":"store.store@company.com","reason":"testing purpose.","secondaryContactName":"name here test","priority":"medium","secondaryContactNumber":"987-654-9999","primaryContactEmail":"mgr.store@company.com","primaryContactNumber":"1234","primaryContactName":"Dave   test","custom1":"INC00123453","custom2":"Store Number 123"}}');

var response = request.execute();

gs.log('API Status v2: ' +response.getStatusCode());

gs.log('API Response v2: ' +response.getBody());

Now the company I need to send the data to have built their own ITSM tool and they need the data sent as 'x-www-form-urlencoded' and they must have the payload sent with {data:{ before the fieldnames:values.

I can't seem to get past the response message of '{"message":"data is not valid."}' / status: 400

Can anyone help?

1 ACCEPTED SOLUTION

dave_edgar
Mega Guru

var template = {'message':'Message','secondaryContactEmail':'email@address.com','reason':'Reason','secondaryContactName':'anotherContact','priority':'medium','secondaryContactNumber':'00000000000','primaryContactEmail':'email@address.com','primaryContactNumber':'00000000000','primaryContactName':'name','custom1':'incidentId','custom2':'customfield'};



template.message = current.short_description.getDisplayValue();


template.reason = current.description.getDisplayValue();


template.primaryContactEmail = current.caller_id.u_4th_email.toString(); //store email address


template.primaryContactNumber = current.caller_id.phone.toString(); //business phone


template.primaryContactName = current.u_store_user_name.getDisplayValue();


template.secondaryContactName = '';


template.secondaryContactNumber = current.caller_id.u_other_phone.toString(); //other phone number field


template.secondaryContactEmail = current.caller_id.u_2nd_email.toString(); //2nd email/mgr email address field


template.custom2 = current.caller_id.user_name.toString(); //caller username id


template.custom1 = current.number.getDisplayValue();



var body = new JSON().encode(template);


gs.log('eCW API Request Body Encoded : ' + body);



var request = new sn_ws.RESTMessageV2();


request.setEndpoint('<endPoint>createOrUpdateTicket');


request.setHttpMethod('POST');


request.setRequestHeader('authCode','1234567');


request.setRequestHeader('Accept','application/json');


request.setRequestHeader('Content-Type','application/json');


request.setRequestBody('data='+ body);




This worked


View solution in original post

5 REPLIES 5

dave_edgar
Mega Guru

var template = {'message':'Message','secondaryContactEmail':'email@address.com','reason':'Reason','secondaryContactName':'anotherContact','priority':'medium','secondaryContactNumber':'00000000000','primaryContactEmail':'email@address.com','primaryContactNumber':'00000000000','primaryContactName':'name','custom1':'incidentId','custom2':'customfield'};



template.message = current.short_description.getDisplayValue();


template.reason = current.description.getDisplayValue();


template.primaryContactEmail = current.caller_id.u_4th_email.toString(); //store email address


template.primaryContactNumber = current.caller_id.phone.toString(); //business phone


template.primaryContactName = current.u_store_user_name.getDisplayValue();


template.secondaryContactName = '';


template.secondaryContactNumber = current.caller_id.u_other_phone.toString(); //other phone number field


template.secondaryContactEmail = current.caller_id.u_2nd_email.toString(); //2nd email/mgr email address field


template.custom2 = current.caller_id.user_name.toString(); //caller username id


template.custom1 = current.number.getDisplayValue();



var body = new JSON().encode(template);


gs.log('eCW API Request Body Encoded : ' + body);



var request = new sn_ws.RESTMessageV2();


request.setEndpoint('<endPoint>createOrUpdateTicket');


request.setHttpMethod('POST');


request.setRequestHeader('authCode','1234567');


request.setRequestHeader('Accept','application/json');


request.setRequestHeader('Content-Type','application/json');


request.setRequestBody('data='+ body);




This worked