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

Chris M3
Tera Guru

The formats don't match.   They are saying it must be urlencoded which means field1=value&field2=value, but then they are asking you to send the payload as json formatted?   You need to work with them to clarify.



Always validate the API format through a REST tool also before writing the script.   There are many chrome extensions that support sending REST calls, I currently use ARC (Advanced Rest Client)


tried as JSON as well but that also fails.



We testing in Postman before putting into SNow and the only way we could get PM to send the data was to specify the key



Screen Shot 2017-04-21 at 15.29.00.png



do I need to do that in the script also?   I don't know enough about api's clearly


So, the body may need to be something like data={"message":..



You might run into issues with special characters and needing to escape things, but I would give that a try.   If postman can show you exactly what the body looks that is being sent, that might provide some clues.


sadly I still get a response of {"message":"data is not valid."}