function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Yogesh BiyaniYogesh Biyani 

HTTP 405 method not allowed

I have converted the following python script to apex code and I get the HTTP 405 method not allowed error. Can someone review and let me know what is wrong? 

Python  
url = 'https://amplitude.com/api/2/events/segmentation'

# json parameters for querying monthly uniques for Launched App  between dates
# Add gp: in front of custom user_property
params1 = {
    'e': '{"event_type":"Launched Application","filters":[{"subprop_type":"user","subprop_key":"gp:app_name","subprop_op":"is","subprop_value":["su-desktop"]}, \
         {"subprop_type":"user","subprop_key":"gp:sku","subprop_op":"contains","subprop_value":["PRO"]}]}',
    'm': 'uniques',
    'start': '20190101',
    'end': '20200131',
    'i':'30'
}

# Set to -300000, -3600000, 1, 7, or 30 for real-time, hourly, daily, weekly, and monthly counts,



r = requests.get(url, params = params1,  auth=(api_key, secret_key))
response = r.json()
print( response)
#print r.status_code
#print r.url
Apex
 
HttpRequest req4= new HttpRequest();
req4.setEndpoint('https://amplitude.com/api/2/events/segmentation');
req4.setMethod('GET');
Blob myBlob1 = Blob.valueof(api_key + ':'+ secret_key);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(myBlob1);
req4.setHeader('Authorization', authorizationHeader);

String params1='{"e": "{\"event_type\":\"Launched Application\",\"filters\":[{\"subprop_type\":\"user\",\"subprop_key\":\"gp:app_name\",\"subprop_op\":\"is\",\"subprop_value\":[\"su-desktop\"]},{\"subprop_type\":\"user\",\"subprop_key\":\"gp:sku\",\"subprop_op\":\"contains\",\"subprop_value\":[\"PRO\"]}]}","end": "20200131","i": "30","m": "uniques","start": "20190101"}';


req4.setBody(params1);

Http http4 = new Http();
HTTPResponse res4 = http4.send(req4);
System.debug(res4.getBody());
VinayVinay (Salesforce Developers) 
Hi Yogesh,

You might have missed to return res4 please check.

HTTPResponse res4 = http4.send(req4);
return res4;

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar
Yogesh BiyaniYogesh Biyani
Hello Vinay,

I am executing the code in anonymous apex and not in a function so I can't use return.

Yogesh
ayu sharma devayu sharma dev
Hello Yogesh

I saw an example in the Amplitude Documentation (https://help.amplitude.com/hc/en-us/articles/360032842391-HTTP-API-V2#h_a85b2cd1-991b-4faf-bcc0-856153e37201) and they are creating body like
'{"api_key":"API_Key","events":[{"user_id":"12345", "event_type":"watch_tutorial", "user_properties":{"Cohort":"Test A"}, "country":"United States", "ip":"127.0.0.1", "time":1396381378123}]}'
So have tried giving API_KEY inside the body instead of using basic auth and also remove backslash in the body because in apex double quotations do not require to escape. I am not sure if this is actually the problem, but give it a try and let me know if the problem still persists.

Thanks and Regards
Ayush Sharma
 
Yogesh BiyaniYogesh Biyani
Hello Ayush,

Thanks for the reply. I do not have a user Id.   Meanwhile, I have found that passing json in the endpoint as shown below works.

Regards,
Yogesh
 
HttpRequest req4= new HttpRequest();
            Blob myBlob1 = Blob.valueof(api_key + ':'+ secret_key);
            String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(myBlob1);
            req4.setHeader('Authorization', authorizationHeader);
            
            String endPoint='https://amplitude.com/api/2/events/segmentation?'+
                'e={"event_type":"Launched%20Application","filters":[{"subprop_type":"user","subprop_key":"gp:app_name","subprop_op":"is","subprop_value":["desktop"]},'+
                '{"subprop_type":"user","subprop_key":"gp:sku","subprop_op":"contains","subprop_value":["Sample"]},'+
                '{"subprop_type":"user","subprop_key":"gp:account_id_list","subprop_op":"contains","subprop_value":["'+fchId+
                '"]}]}&m=uniques&start=20190101&end=20200301&i=30';
            
            req4.setEndpoint(endPoint);
            req4.setMethod('GET');

            
            Http http4 = new Http();
            HTTPResponse res4 = http4.send(req4);
            System.debug(res4.getBody());