首页 > 解决方案 > 根据我的代码为 APEX 制作卷曲标注

问题描述

我知道之前有人问过这个问题:

https://salesforce.stackexchange.com/questions/67867/novice-question-making-a-curl-callout-from-apex-to-a-3rd-party-app

但是 Curl 的格式与我的不一样,所以我想知道如何在 Apex 端点编程中转换它?

curl H "Content-Type:application/json" -H "charset:UTF-8" -H "Authorization:Bearer [access token]" -X POST -d '{[body]}' [endpoint]

我的代码:

public static httpResponse callout(String httpMethod, String endpoint, String body){
        //Instantiate an httpRequest and set the required attributes
        httpRequest req = new httpRequest();
        req.setMethod(httpMethod);
        req.setEndpoint(endpoint);

        //Optional attributes are often required to conform to the 3rd Party Web Service Requirements
        req.setHeader('Content-Type', 'application/json; charset=utf-8');
        req.setHeader('Authorization','Bearer' + 'xxxxxxxxxxxx[this is the access token]');
        req.setBody('taken the body');

        //You can adjust the timeout duration (in milliseconds) to deal with slow servers or large payloads
        req.setTimeout(120000);

        //Use the HTTP Class to send the httpRequest and receive an httpResposne
        /*If you are not using an HttpCalloutMock: 
        if (!test.isRunningTest){
        */
        httpResponse res = new http().send(req);
        /*If you are not using an HttpCalloutMock: 
        }
        */
        system.debug(res.toString());
        system.debug(res.getBody());
        return res;
    }

我收到了内部错误的错误 500,我不知道为什么。

标签: curlsalesforceapex

解决方案


推荐阅读