5 Replies · Latest reply on May 4, 2017 2:47 AM by Dave Edgar

    How can api payload as {data:{ ?!

      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?

      Dave

        • Re: How can api payload as {data:{ ?!
          Chris Martin

          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)

          • Re: How can api payload as {data:{ ?!
            Dave Edgar

            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

            Dave