I am making a call to Google Analytics from salesforce. I am getting an error stating that the parameter 'v' is not being sent. This is included in the json body.
If I use a third party script like python to send the same parameters via an HTTP Post request, I am able to send them over. Yet, sending these parameters via Salesforce Apex is causing this error. Here is my python code that works.
import requests
from collections import OrderedDict
geoids='1000041'
referral='http://www.bing.com'
uid='GOOGLEUACODE'
payload = {"tid" : uid, "v" : "1", "cid" : "client_1", "t" : "event", "dp" : "www.bing.com", "ds" : "web", "dr" : "referral", "geoid" : "1000041", "ec" : "web", "ea" : "web", "el" : "web", "ev" : "0"}
urls='https://www.google-analytics.com/debug/collect'
resp = requests.post(url=urls,params=payload,headers=user_agent)
print resp
print resp.url
print resp.status_code
If I attempt this exact same call in Apex, it fails with an error saying the paramter "v" has not been supplied. The exact error message is
"description": "A value is required for parameter 'v'. Please see http://goo.gl/a8d4RP#v for details.",
Here is the Apex code for this.
string url= 'https://www.google-analytics.com/debug/collect';
string payload = '{"tid" : "GOOGLEUACODE", "v" : "1", "cid" : "client_1", "t" : "event", "dp" : "www.bing.com", "ds" : "web", "dr" : "referral", "geoid" : "1000041", "ec" : "web", "ea" : "web", "el" : "web", "ev" : "0"}';
Http h = new Http();
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
req.setEndpoint(url);
req.setMethod('POST');
req.setBody(payload);
res = h.send(req);
System.Debug('response body '+res.getBody());
It looks like I am unable to send the body in the POST method. I have tried a few things like setting the Content-Type in the HTTP Request, but those have not worked.
What has worked is changing the method to a GET and converting the payload string into a different format like tid=GOOGLECODE&v=1&event=dp
So how can I preserve the format of the payload string in the original python code and do this?
If you want to try this code, you can use this UA-ID created by me for this purpose: uid='UA-78999084-1'
Edited: I am not trying to make an actual call in the code. The validation succeeds in the python code but not in the Apex code.