根据我的代码为APEX进行卷曲标注

时间:2019-01-16 01:25:53

标签: curl salesforce 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,但我不知道为什么。

0 个答案:

没有答案