2

I'm trying to use the SOAP Metadata API using NodeJS with the soap npm package.

I successfully log in using the enterprise API and get the session ID, but when I go to use the metadata API, I run into two different errors depending on what I try.

Here's the code:

    soap.createClient(enterprise_wsdl, function(err, ent_client){

      if(err){
        console.log('Enterprise Error:');
        console.log(err);
      }

      ent_client.login(loginOptions, function(err, loginResult){
        if(err){
          console.log('Login Error:');
          console.log(err);
        }

///////////////THIS LINE
        var server = loginResult.result.serverUrl;
///////////////
        console.log(loginResult.result.metadataServerUrl);
        console.log(loginResult.result.serverUrl);
        var session = loginResult.result.sessionId;

        soap.createClient(metadata_wsdl, function(err, met_client){
          if(err){
            console.log('Metadata Error.');
            console.log(err);
          }


          met_client.setEndpoint(server);
          var sessionHeader = {SessionHeader: {sessionId: session}};
          met_client.addSoapHeader(sessionHeader,'','tns','urn:partner.soap.sforce.com');

          console.log(met_client.describe());

          met_client.retrieve(retrieveOptions, function(err, retrieveResult){
            if(err){
              console.log('Retrieve Error.');
              console.log(err.body);
            }
            else{
              console.log(retrieveResult);
            }
          });

        });


      });

    });

When I run that code as is, I get the following output:

https://na35.salesforce.com/services/Soap/m/38.0/(orgid)
https://na35.salesforce.com/services/Soap/c/38.0/(orgid)
{ MetadataService: 
   { Metadata: 
      { cancelDeploy: [Object],
        checkDeployStatus: [Object],
        checkRetrieveStatus: [Object],
        createMetadata: [Object],
        deleteMetadata: [Object],
        deploy: [Object],
        deployRecentValidation: [Object],
        describeMetadata: [Object],
        describeValueType: [Object],
        listMetadata: [Object],
        readMetadata: [Object],
        renameMetadata: [Object],
        retrieve: [Object],
        updateMetadata: [Object],
        upsertMetadata: [Object] } } }
Retrieve Error.
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><soapenv:Fault><faultcode>soapenv:Client</faultcode><faultstring>No operation available for request {http://soap.sforce.com/2006/04/metadata}retrieve</faultstring></soapenv:Fault></soapenv:Body></soapenv:Envelope>

No operation available for request {http://soap.sforce.com/2006/04/metadata}retrieve

When I change the indicated line to use the .metadataServerUrl instead, I get the following output:

https://na35.salesforce.com/services/Soap/m/38.0/(orgId)
https://na35.salesforce.com/services/Soap/c/38.0/(orgId)
{ MetadataService: 
   { Metadata: 
      { cancelDeploy: [Object],
        checkDeployStatus: [Object],
        checkRetrieveStatus: [Object],
        createMetadata: [Object],
        deleteMetadata: [Object],
        deploy: [Object],
        deployRecentValidation: [Object],
        describeMetadata: [Object],
        describeValueType: [Object],
        listMetadata: [Object],
        readMetadata: [Object],
        renameMetadata: [Object],
        retrieve: [Object],
        updateMetadata: [Object],
        upsertMetadata: [Object] } } }
Retrieve Error.
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sf="http://soap.sforce.com/2006/04/metadata"><soapenv:Body><soapenv:Fault><faultcode>sf:INVALID_SESSION_ID</faultcode><faultstring>INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session</faultstring></soapenv:Fault></soapenv:Body></soapenv:Envelope>

INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session

From what I can tell, when I try to hit the serverUrl, it does not support the Metadata API operations, but when I try to hit the metadataServerUrl, I get an invalid session, indicating that I haven't logged in, despite setting the SessionID in the exact same way.

I've tried multiple operations from the metadata API against serverUrl and none are supported.

1 Answer 1

2

You need to use the metadataServerUrl, and you also need to use the metadata API types/namespaces when creating the session header, e.g.

met_client.setEndpoint(loginResult.result.metadataServerUrl);
var sessionHeader = {SessionHeader: {sessionId: session}};
met_client.addSoapHeader(sessionHeader,'','tns','http://soap.sforce.com/2006/04/metadata');
1
  • Thanks! This got me past that error onto a different one, but I'll work through it from here! Thanks for the help! Commented Dec 27, 2016 at 21:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .