i need to make an HTTP Request with Apex to the Star Wars Api but keep getting an error status 301 in the response. I don't know what to do anyomre, it's been a few days. If I make a fetch in Java Script to the SWAPI or make a request in Apex to another API, it works just fine. Here's my code:

public with sharing class SWController {
    public static Object getContactData(integer id){
        List<Object> contact = new List<Object>();
        Http h = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://swapi.dev/api/people/' + id);
            HttpResponse response = h.send(request);
            If(response.getStatusCode() == 301 || response.getStatusCode() == 302)
                response = new Http().send(request);
                if (response.getStatusCode() == 200) {
                Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
                Map<String, Object> name = (Map<String, Object>) results.get('name');
            } else {
            } catch(Exception e){
        throw e;
        return contact;

Then I console.log(contact) in the Aura controller to check the status code and it's always 301. If I use the debug console, it's also 301 always in the log. I tested the API in SAOP UI and get a status code of 200 (just like in Java Script). I also enabled the site in the Access Remote Site setup.

Thanks in advance. I'm new to Apex so sorry if it's a silly question. Been looking everywhere for an answer.

2 Answers 2


From playing around just a little, it appears that the issue is that you're missing a trailing '/'

The url should be https://swapi.dev/people/id/<integer>/.

Omitting that appears to generate the 301, which appears to point to the correct url (location header) but using http instead of https. The site has HSTS enabled (HTTP Strict Transport Security) so http connections can be flat out rejected.

In this case, it looks like an http request gets you another redirect (and it looks like the second redirect gets you the proper url to use).

In the end though, just make sure you add that trailing slash.

This wasn't really a Salesforce thing, outside of Salesforce not automatically following redirects. I was able to confirm the behavior using cURL (locally, from Terminal (using Ubuntu) --fail-early to prevent it from auto-following redirects, and -I to get at the headers of the response)


In documentation https://swapi.dev/documentation#people its written that the api ends with '/'. So your endpoint becomes:

request.setEndpoint('https://swapi.dev/api/people/' + id + '/');

Instead of

request.setEndpoint('https://swapi.dev/api/people/' + id);

You must log in to answer this question.

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