1

I am calling sample google book URL from the lightning component, For that i have written APEX controller to make http request . But it is throwing 404 error for the Google Book API. Here is my APEX controller please check ,

public  class WebservicesController {
     public static String responseFormat='application/json';
     public static String bodyContentType='application/json';

@AuraEnabled
public static Response makeRequest(String url,  String method, String bodyContent) {
    System.debug('Making request httpResponse ' ); 
    HttpRequest request = buildRequest(url, method,bodyContent);
    HttpResponse httpRes = sendRequest(request);
    Response restRes = buildResponse(httpRes);
    return restRes;
}

private static HttpRequest buildRequest(String url, String method, String bodyContent) {
    HttpRequest request = new HttpRequest();
    System.debug('Making request httpResponse '+ url ); 
    request.setEndpoint(url);
    request.setMethod(method);
    request.setHeader('Content-Security-Policy', '*'); 

    if (bodyContent != null) {
        request.setBody(bodyContent);
        request.setHeader('Content-Type', bodyContentType); 
    }

    request.setHeader('ACCEPT', responseFormat);

    return request;
}

private static HttpResponse sendRequest(HttpRequest request) {    
    return new Http().send(request);
}

private static Response buildResponse(HttpResponse httpRes) {
    Response restRes = new Response();

    restRes.status = httpRes.getStatus();
    restRes.statusCode = httpRes.getStatusCode();
    restRes.body = httpRes.getBody();
            System.debug(' Status code is  ' + restRes.statusCode  ); 

    System.debug(' httpResponse ' + httpRes.getBody() ); 

    return restRes;
}

public class Response {

    @AuraEnabled
    public String status { get; set; }

    @AuraEnabled
    public Integer statusCode { get; set; }

    @AuraEnabled
    public String body { get; set; }
}

}

and also my helper js controller where i am calling this apex controller is method is below ..

 createCustomer: function(component, customer) {
    var action = component.get("c.makeRequest");        
    action.setParams({
        url: "https://www.googleapis.com/books/v1/volumes/NFPqCQAAQBAJ",
        method: "GET",
        bodyContent: "",
    });
    action.setCallback(this, function(a) {

        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                var customers = component.get("v.data");
                customers.push(response.getReturnValue());
                component.set("v.data", customers);
            }
             var action = component.get("c.saveCustomer");
                action.setParams({
                    "customer": customer
        });
        });
        $A.enqueueAction(action);
    });
    $A.enqueueAction(action);
},

And i also given this google api url in Remote settings and also added as CSP trusted site in my domain.

4

1 回答 1

0

在以下方法前添加@AuraEnabled:

  • 构建请求
  • 发送请求
  • 构建响应
于 2018-09-20T06:54:32.570 回答