Send a base 64 file with rest message multipart/form-data

fabrizio95360
Tera Expert

Hi, I have to send a base 64 file with rest message, in my code i did this: 

 

 

 

var body = 'grant_type=client&client_id=test45&client_secret=djfhfhfh&scope=test35';
var r = new sn_ws.RESTMessageV2('rest goSign token', 'POSTTOKEN');
r.setRequestBody(body);
var response = r.execute();
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();

        if (httpStatus === 200) {
			gs.info('http status: ' + httpStatus);
            gs.info('token: ' + responseBody);
            
        } else {
            gs.info('Error Assertion: ' + responseBody);
            gs.error(' http status: ' + httpStatus);
        }

var token = responseBody.access_token;
var template = {
  envelope: {
    subject: "Subject of the dossier",
    loadedFor: "mario.rossi",
    externalId: "EXT_ID_ENVELOPE_16",
    useType: "CORPORATE",
    sendingMode: "IMMEDIATE",
    documents: [
      {
        externalId: "DOCEXTID_16",
        title: "Title of the file",
        mimeType: "application/pdf",
        originalFname: "Modulo pc.pdf"
      }
    ],
    procDef: {
      tasks: [
        {
          type: "HUMAN",
          order: 1,
          signatureType: "SIMPLE",
          actorExpr: "mario.rossiU",
          action: "WORK",
          howMany: 1,
          actionDefs: [
            {
              docExternalId: "DOCEXTID_16",
              type: "SIGN",
              appearance: {
                coordAppearance: {
                  page: 1,
                  llx: 167,
                  lly: 680,
                  urx: 347,
                  ury: 725
                }
              }
            }
          ]
        }
      ]
    }
  }
};
var bodyEnvelope = JSON.stringify(template);

var r2 = new sn_ws.RESTMessageV2('rest goSign token', 'POSTENVELOP');
r2.setRequestHeader("Authorization", "Bearer " + token);
r2.setRequestBody('body='+bodyEnvelope + 'DOCEXTID_16=JVBERi0xLjcNJeLjz9MNCjIgMCBvYmoNPDwvUGFnZXMgMSAwIFIvVHlwZS9DYXRhbG9nPj4NZW5kb2JqDTMgMCBvYmoNPDwvQ3JlYXRpb25EYXRlKEQ6MjAyM..............'); //encoded file in base64

var response2 = r2.execute();
        var responseBody2 = response2.getBody();
        var httpStatus2 = response2.getStatusCode();

        if (httpStatus2 === 200) {
			gs.info('http statusEnvelop: ' + httpStatus2);
            gs.info('Envelop: ' + responseBody2);
            
        } else {
            gs.info('Error Envelop: ' + responseBody2);
            gs.error(' http statusEnvelop: ' + httpStatus2);
        }

 

 

 

in my rest message: 

fabrizio95360_0-1694510551693.png

 

but it keeps giving me a 404 error, how can I solve it?

 

thanks in advance

 

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@fabrizio95360 

error 404 means the resource is not present

Did you try testing that endpoint from postman etc an external tool?

 

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

Yes i tried in postman and it works but in my script not working

S Goutham
Tera Guru

Hey @fabrizio95360 :

The 404 error is when your endpoint is not reachable did you try hitting the API from a postman to validate the access token and credentials you are using 

I see you have used a static encode base64 string if it needs to be dynamically built using the String Util method is ServiceNow  "Base64encode" or "Base64decode"

https://john-james-andersen.com/blog/service-now/easy-base64-encoding-in-servicenow.html

 

I hope this solves your issue
Mark this as Helpful / Accept the Solution if this clears your issue

thanks Goutham, right now I need to send a static file, could the problem be that multipart form-data is not suported by servicenow? because the endpoint on postman is working with the same token