I am trying to post some text and image to Pinterest by using inappbrowser, but its throwing an error "Parameter 'image_url' (value http:null) is not a valid URL format."
Here is the sample code.
var pinterestUrl = "http://www.pinterest.com/pin/create/button/";
pinterestUrl += "?url=https://www.google.co.in/";
pinterestUrl += "&media=http://www.google.co.ma/images/srpr/logo1w.png";
pinterestUrl += "&description=Text Description";
var pinterest = window.open(pinterestUrl, '_blank');
It's working fine in web browser and in system browser (iPhone/Android) if I change the code from "_blank" to "_system". I treid to inspect the url by using the eventListener "loadstart" and found that inappbrowser automatically adding some unnecessary extra parameters like "create next button".
Any suggestion would be helpful.
Per our conversation in the comments, I believe the issue is with no encoding your query parameters. The encodeURIComponent
method is used to encode special characters when used within queries. Encoding the entire url will not work because all of the special characters will be encoded. You should only encode the query parameter values themselves.
var pinterestUrl = "http://www.pinterest.com/pin/create/button/";
pinterestUrl += "?url=" + encodeURIComponent("https://www.google.co.in/");
pinterestUrl += "&media=" + encodeURIComponent("http://www.google.co.ma/images/srpr/logo1w.png");
pinterestUrl += "&description=" + encodeURIComponent("Text Description");
var pinterest = window.open(pinterestUrl, '_blank');
Here is another way to write the same thing that might be a little easier to read.
function buildUrl(baseUrl, queryParams) {
return Object.keys(queryParams).reduce(function(url, key) {
return key + '=' + encodeURIComponent(queryParams[key]);
}, baseUrl + '?');
}
function shareToPinterest() {
var queryParams = {
url: "https://www.google.co.in/",
media: "http://www.google.co.ma/images/srpr/logo1w.png",
description: "Text Description"
};
var pinterestUrl = buildUrl("http://www.pinterest.com/pin/create/button/", queryParams);
var pinterest = window.open(pinterestUrl, '_blank');
}