+ Start a Discussion
Martin Cadman 2016Martin Cadman 2016 

Superbadge: Data Integration Specialist

Challenge #3
After a number of mis-starts, I have passed this challenge. However, I am not convinced that my code is doing what it should (or anything!).

If I don't specifiy the full https path in my callout (and just use the named credential as I beleive I should) I get an unathorized error:
14:28:29:005 CALLOUT_REQUEST [24]|System.HttpRequest[Endpoint=callout:ProjectService, Method=POST]
14:28:29:348 CALLOUT_RESPONSE [24]|System.HttpResponse[Status=Unauthorized, StatusCode=401]

The details in the named credential appear fine (I can use them and login manually).

If I include the URL in the callout (!!!?), I appear to authorize ok - however the response 'Not Found' is the same result I get if I connect to the site and logon manually:
13:36:08:006 CALLOUT_REQUEST [24]|System.HttpRequest[Endpoint=callout:ProjectService/https://sb-integration-pms.herokuapp.com/projects, Method=POST]
13:36:08:393 CALLOUT_RESPONSE [24]|System.HttpResponse[Status=Not Found, StatusCode=404]

I'm sure I'm doing something silly here but can't spot it. 

BTW
I also passed the challenge with no code to change the opportunity stage to submitted project/resubmit project. This doesn't matter but building this code does make repeated tests faster.
Martin Cadman 2016Martin Cadman 2016
Further to my original question, I assume I'm doing something wrong with the token header setting in the call - but I can see what that might be:
ServiceTokens__c ST = ServiceTokens__c.getvalues('ProjectServiceToken');
request.setHeader('token', ST.Token__c);

 
Alexandre Lachmann (SF)Alexandre Lachmann (SF)
I have a System.CalloutException: Read timed out with this code

        HttpRequest req = new HttpRequest();
        req.setEndpoint('callout:ProjectService/projects');
        req.setHeader('token', '0661666e-8a92-xxx-a162-a977ce6826f2');
req.setHeader('Content-Type', 'application/json');
        String body='{"opportunityId":"00641000004EKmFAAW","opportunityName":"Super-Duper Big Deal","accountName":"ACME Corp, Inc.","closeDate":"2016-10-30","amount":150000}';
        req.setBody(body); //array buffer
        req.setMethod('POST');
        Http http = new Http();
        HTTPResponse res = http.send(req);
        System.debug('Body:'+res.getBody());

Or 
Body:Bad Request. Your JSON does not contain the correct case-sensitive keys. Please reference the sample JSON.with this code
        HttpRequest req = new HttpRequest();
        req.setEndpoint('callout:ProjectService/projects');
        req.setHeader('token', '0661666e-8a92-4287-a162-a977ce6826f2');
req.setHeader('Content-Type', 'JSON');
        String body='{"opportunityId":"00641000004EKmFAAW","opportunityName":"Super-Duper Big Deal","accountName":"ACME Corp, Inc.","closeDate":"2016-10-30","amount":150000}';
        req.setBody(body); //array buffer
        req.setMethod('POST');
        Http http = new Http();
        HTTPResponse res = http.send(req);
        System.debug('Body:'+res.getBody());

Any idea ?
Alexandre Lachmann (SF)Alexandre Lachmann (SF)
now I have 400 instead of time out with first code req.setHeader('Content-Type', 'application/json'); !!!!
Martin Cadman 2016Martin Cadman 2016
A few things discovered along the way

setHeader('Content-Type', 'application/json') is required
401 error above - bad password in the Named Credential
404 error above - specifying the URL as well as the Named Credential is a stupid thing to do
 
Alexandre Lachmann (SF)Alexandre Lachmann (SF)
now it works after recreating my token : very strange since it is used for callin and not callout
Martin Cadman 2016Martin Cadman 2016
500 Internal Server Error - check that you token is correct and in the right format.
Abhishek Soni 13Abhishek Soni 13
I am stuck with challange#3 and getting System.HttpResponse[Status=Unauthorized, StatusCode=401]
I tried both Named Credentials and Remote site setting but getting same error
System.HttpRequest[Endpoint=https://sb-integration-pms.herokuapp.com/projects, Method=POST]
System.HttpRequest[Endpoint=callout:ProjectService, Method=POST]

I re-registerd the org and tried with new token as well but no success.

I am surprised that I have completed challege#3 even i am getting error Status=Unauthorized, StatusCode=401 while trying to connect PMS.
Any thoughts what mistake am i making?
Kumar MydKumar Myd
Its been a while ... getting the error "
It doesn't appear that the named credential is being used to set the endpoint in the 'ProjectCalloutService' class.
Close errors"

I have the named credential and token setup correct. 
Also the code is        
       ServiceTokens__c serviceToken = ServiceTokens__c.getvalues('ProjectServiceToken');
        String ST = serviceToken.Token__c;
        req.setEndpoint('callout.ProjectService');
        req.setMethod('POST'); 
        req.setHeader('token', ST);
        req.setHeader('Content-Type', 'application/json;charset=UTF-8');

Any idea ?
Dmitriy GribanovDmitriy Gribanov
Hey Kumar Myd! If this is still actual for you - you have dot instead of colon here (between 'callout' and 'ProjectService'): req.setEndpoint('callout.ProjectService').
Hope this will help :)
Dmitriy GribanovDmitriy Gribanov
BTW, it is quite strange but logic seems to be working properly when both URL in named credentials and request.setEndpoint() method in callout contains path '/projects', i.e. request.setEndpoint('callout:ProjectService/projects'); - in code and URL https://sb-integration-pms.herokuapp.com/projects - in named credentials...