2

When I load my web app via Phonegap and make a web request (via AJAX or otherwise), the REFERRER HTTP header is not set at all. This is interfering with the functionality of some third-party websites. How can I make the REFERRER header get sent?

(I am using Phonegap 3.5.0-0.20.10)

asdf
  • 153
  • 1
  • 5

2 Answers2

2

As told by @JamesWong, now there is a plugin named cordovaHttp available for all platforms.

You can add that plugin using cordova plugin add https://github.com/wymsee/cordova-HTTP.git command and then you can add any http header as below.

cordovaHTTP.post("https://google.com/, {
    id: 12,
    message: "test"
}, { Authorization: "OAuth2: token" }, function(response) {
    // prints 200
    console.log(response.status);
    try {
        response.data = JSON.parse(response.data);
        // prints test
        console.log(response.data.message);
    } catch(e) {
        console.error("JSON parsing error");
    }
}, function(response) {
    // prints 403
    console.log(response.status);

    //prints Permission denied 
    console.log(response.error);
});

Source: https://github.com/wymsee/cordova-HTTP

IsmailS
  • 10,797
  • 21
  • 82
  • 134
  • Hi @IsmailS, I have a new Cordova application. Once I add this plugin, where do I actually put/write the Javascript to invoke the `cordovaHTTP` library and set a custom header? – mecampbellsoup Aug 04 '15 at 17:01
  • @mecampbellsoup you don't need to invoke cordovaHttp. It will be directly accessible when you run application on your device. But it won't be accessible when you run in browser. You have to directly call it where you would otherwise do an ajax call. In the above code, third parameter take headers. There you can pass referer header. – IsmailS Aug 04 '15 at 19:22
  • Salam @IsmailS, can I make headers default to the native request, like bring the images – Eymen Elkum Dec 07 '16 at 07:04
  • If you mean the request generated by ``, then not possible. – IsmailS Dec 08 '16 at 09:22
  • 1
    @IsmailS does this add "referer" to the request? if yes, it does not work for me. – Roni Litman Apr 08 '18 at 14:04
  • @ronilitman [there are three parameters for post function](https://github.com/wymsee/cordova-HTTP#post). The 3rd one is headers. You can pass referrer from there... same as in the above example, `Authorization` header is passed – IsmailS Apr 17 '18 at 07:16
1

HTTP Requests are handled via webview/ chromeview (Android) and UIWebView (iOS). It is not possible to change it on JS/ HTML level. I reckon you might be able to achieve it by tweaking the Cordova layer, the down side to this is, you will have to do it for all the platforms you are supporting.

See this:

You can probably write a plugin which interfaces with your JS/ HTML codes to determine when to send out the custom HTTP REFERRER.


EDIT

For iOS, there's a clean sample code with exactly what you need posted here:

Community
  • 1
  • 1
James Wong
  • 4,529
  • 4
  • 48
  • 65
  • Thanks for your comment. The answer you linked to provides the solution for android, but how should I go about doing it on iOS? – asdf Aug 05 '14 at 23:34
  • Hello @asdf I just added a link to iOS that does exactly this. Post back with your progress. – James Wong Aug 06 '14 at 01:51
  • Ah, I have realized with your second android link that it is for loading a URL into a WebView. I am using Javascript (namely JQuery's ajax) to do load the content. – asdf Aug 06 '14 at 02:03