1

Related to this other question Discover Schema of Source + Metadata XML formats I am trying to download the Metadata API WSDL file from the Orgs URL https://ServInstance.salesforce.com/services/wsdl/metadata using this simple Apex code:

public Dom.Document mdApiWsdl() {
    HttpRequest request = new HttpRequest();
    request.setEndpoint(URL.getOrgDomainUrl().toExternalForm() + '/services/wsdl/metadata');
    request.setMethod('GET');
    request.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
    request.setHeader('Content-Type', 'application/xml');

    HttpResponse response = new Http().send(reqquest);
    return response.getBodyDocument();
}

The issue I face is that the page is not a WSDL XML document but a strange HTML page simulating it. In a browser, it's possible to download the file as XML but in Apex code I couldn't find a way.

Why is Salesforce preventing us from downloading this single WSDL?

6
  • 1
    In Apex did you try reading it via XmlStreamReader ? Commented Aug 8, 2021 at 10:30
  • Yes I did but it an HTML page that pretends to be an XML. Commented Aug 8, 2021 at 10:33
  • 1
    Ok I see now. So you want a way to parse the response in XML format from Apex? Commented Aug 8, 2021 at 10:44
  • 1
    I understood the issue, but dont know how to solve this. So the same response comes if you use a postman too. The only time it works if you already have logged in to the browser and your session is active. Then it returns you the xml format. Now I am not sure how to pass creds in the header, I tried everything but nothing works. Commented Aug 8, 2021 at 14:25
  • Are you sure this isn't an authentication error html page because you didn't include the sid cookie in the request? Commented Aug 8, 2021 at 18:18

1 Answer 1

1

The particular page is expecting a sid session cookie rather than an Authorization header. You'll need a full UI session and then pass that session id into the Cookie header.

E.g.

HttpRequest request = new HttpRequest();
request.setEndpoint(URL.getOrgDomainUrl().toExternalForm() + '/services/wsdl/metadata');
request.setMethod('GET');
//request.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
request.setHeader('Cookie','sid='+UserInfo.getSessionId());
request.setHeader('Content-Type', 'application/xml');

HttpResponse response = new Http().send(request);
System.debug(response.getBody());

You must log in to answer this question.

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