Courseiva

SF-PD2 Process Automation, Logic, and Integration Practice Question

Exhibit

public class IntegrationService {
  public static void processData() {
    HttpRequest req = new HttpRequest();
    req.setEndpoint('https://api.external.com/data');
    req.setMethod('POST');
    Http h = new Http();
    HttpResponse res = h.send(req);
  }
}

Refer to the exhibit. The service is failing because the external system requires a specific 'Content-Type' header that is missing. Which addition to the code will correctly resolve this issue?

⚠ Common exam trap

Candidates often assume the HttpRequest object is automatically configured for JSON. They forget that external systems strictly validate headers, leading to failures if the Content-Type is missing or incorrectly set.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

req.setHeader('Content-Type', 'application/json');

To successfully communicate with an external API that expects specific metadata, the HTTP request must be configured with the necessary headers before the request is sent. The HttpRequest class provides the setHeader() method, which is the standard way to define metadata such as 'Content-Type' or 'Authorization'. Adding this line ensures the request is formatted correctly for the external system's expectations.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✓

    req.setHeader('Content-Type', 'application/json');

    Why this is correct

    Setting the 'Content-Type' header explicitly informs the external server about the format of the data being sent. This is a standard requirement for RESTful APIs and ensures the server correctly parses the request body, preventing 415 Unsupported Media Type or similar errors in the integration handshake.

  • ✗

    req.setBody('Content-Type: application/json');

    Why it's wrong here

    Putting header information into the body is incorrect because the request body is reserved for the payload data. External servers expect headers to be in the HTTP metadata section. Placing it in the body will result in a malformed request that the target API will likely reject.

  • ✗

    req.setMethod('Content-Type: application/json');

    Why it's wrong here

    The setMethod() method is used exclusively to define the HTTP verb, such as GET, POST, or PUT. Attempting to include headers or other metadata in this method will cause a runtime error, as the system expects a valid HTTP verb, not a header-value string combination.

  • ✗

    h.setHeader('Content-Type', 'application/json');

    Why it's wrong here

    The setHeader() method is a member of the HttpRequest class, not the Http class. The Http class is used solely for sending the request. Calling a method that doesn't exist on the Http instance will cause a compilation error, preventing the code from being saved or executed.

Visual reference

Client Recursive Resolver Root DNS (13 root servers) TLD DNS (.com, .org, …) Authoritative example.com query IP addr answer

About these practice questions

Courseiva writes every SF-PD2 question from scratch — 226 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Salesforce exam blueprint

This SF-PD2 practice question is part of Courseiva's free Salesforce certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the SF-PD2 exam.