简体   繁体   中英

How to Take User Agent String and Plug Into API URL for Call

Trying to use Glassdoor API to pull JSON data and display/review.

Somewhat related to this question here, except this person used Python and I need to use JS/Jquery: REST: Glassdoor API requires User-Agent in header

However, Glassdoor's API documentation indicates requests are intended to go through this URL/pattern:

http://api.glassdoor.com/api/api.htm?v=1&format=json&t.p=120&t.k=fz6JLNDfgVs&action=employers&q=pharmaceuticals&userip=192.168.43.42&useragent=Mozilla/%2F4.0

And, the user agent string returned by navigator.userAgent is much longer and formatted differently than it would need to be for inclusion in the URL:

Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36

That's what gets returned when I test it in my own setup. I want to make it so the client side user agent is properly added to URL for request.

Do I just use the substring that is the first x number of characters? Is there a better way to automate how the native/client side user agent is being appended to the URL? OR, could I not append it to the URL and submit an object similar to the Python method that contains it and the .getJSON method will automatically do it? I was unclear after checking the JQuery.getJSON doc.

I have tried searching the web, StackOverflow, and github for ideas or a clear JS protocol for how user agent string would be appended/parsed properly to add to URL for .getJSON method in Jquery, but I've come up blank... Or, maybe it just went over my head...

I'm very new and still learning, so maybe I am not looking for the right terms? Or misunderstanding how .getJSON can be used?

Here's the way I have the .getJSON method and URL structured so far:

$.getJSON("http://api.glassdoor.com/api/api.htm?v=1&format=json&t.p=70977&t.k=e5CNsNJMxw7&action=employers&q=" + industry + "&userip=" + uip + "&useragent=Mozilla/%2F4.0")

I figured out how to use ipify.org to get client-side IP-address and drop that into the URL using the variable "uip", so that's what that is.

Many thanks in advance for any help and advice you may have!

When using jQuery, you can provide the parameters as an object, and it will automatically format them properly into the URL:

$.getJSON("http://api.glassdoor.com/api/api.htm", {
    v: 1,
    format: 'json',
    't.p': 70977,
    't.k': e5CNsNJMxw7&action=employers,
    q: industry,
    userip: uip,
    useragent: navigator.userAgent
}, function(response) {
    ...
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM