You need to sign in to do that
Don't have an account?

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
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.urlApex
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());
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
I am executing the code in anonymous apex and not in a function so I can't use return.
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 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
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