0

I have an API which you can access through web url. You can sign in by POSTing your username/password to let's say

http://myapi.example/signin

I do that with a jQUERY's $.post request which works fine - I get the status OK back if everything is correct. Now I need to somehow GET data from this API with certain url, which I would do with $.get of course, but I get the reponse unauthorized… So probably the session somehow gets lost, after the POST request/response.

I know that this POST/GET works, because if I do signin and then just type the GET url in browser - i get the response text plain in browser.

How would you keep the "session" somehow to simulate browser's behaviour in this?

function ajaxSignIn(){
    $.post(url,
    {
        email: document.getElementById("name").value,
        password: document.getElementById("password").value
    },
       function(data,status){  // this is where POST is successful (i get data in)
        console.log("\nStatus of POST: " + status);
        console.log(data);
        $.get(urlForGet,function(data,status){  // GET call
            console.log("Status of GET: " + status);
        });
   });
}

If I was unclear please ask for more info. Thank you for any help.

trainoasis
  • 6,419
  • 12
  • 51
  • 82
  • send authorization parameters along with your GET request and check if they are valid on your server side , OR set the session data in cookies – Aditya Jan 30 '14 at 08:34
  • I can't access the server side, except with my signin details which allows me to get all my data if I type GET request URL after sign in data was posted. – trainoasis Jan 30 '14 at 08:36
  • Are you using GET in POST's onsuccess part? Or seperate? – Kuzgun Jan 30 '14 at 08:38
  • what you've described should work. can you post some code to show the relative calls? are you certain you're not sending the get request until after the past has replied? – BRPocock Jan 30 '14 at 08:40
  • I tried calling GET after POST was complete also. Otherwise i call GET when POST was successful, yes (and when data are back from post) – trainoasis Jan 30 '14 at 08:40
  • Can you explain 'session somehow gets lost' , are the session variables not setting properly? – Aditya Jan 30 '14 at 08:40
  • give us the url of the api so we can actually see what it is. – Georgi-it Jan 30 '14 at 08:57
  • That I can not do I'm afraid (customer thingy, not our own) – trainoasis Jan 30 '14 at 08:59
  • Then, we cannot help you I am afraid. You can give us some screenshots from Fiddler, so we can see the data – Georgi-it Jan 30 '14 at 09:42

2 Answers2

0

I think that your API does not work as it should or you are not using all the data it provides you. Usually this is how these APIs work:

  1. You send login request.
  2. Server replies with status code and if it is ok sends you a generated key which is valid for some period of time.
  3. If you want to use the api further you need to supply this key with every new request.

Browsers use Cookies, APIs use this.

Georgi-it
  • 3,676
  • 1
  • 20
  • 23
  • You might be correct… But where do I get the generated key from and how do I supply it with later requests? The only thing I get back POSTed is some server generated data (no key or something) – trainoasis Jan 30 '14 at 08:51
  • The key should be in the response, or find out how the api works, because there is no way it would work the way you are describing it, except if is not very poorly written. – Georgi-it Jan 30 '14 at 08:52
  • Server DOES maintain a session - but how can I handle client side to stay up to that session without losing it? – trainoasis Jan 30 '14 at 09:39
  • It creates a cookie, but when server response comes back the cookie is lost I see – trainoasis Jan 30 '14 at 09:46
  • Try to get the cookie then - http://stackoverflow.com/questions/12840410/how-to-get-a-cookie-from-an-ajax-response . Also Fiddler screens will help. – Georgi-it Jan 30 '14 at 09:48
0

In case anyone is interested: I ended up sending ajax request to my local php server, where I managed to get a Cookie from the remote API with the use of cURL.

You create a cookie file and then use the cookie with further GET requests …

curl link to cookie explained

Server-side curl part without getting parameters etc etc.:

// here I come from my ajax
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookieJarFilePath); // this is what you need
curl_setopt( $ch, CURLOPT_HEADER, 1);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

// i get data in response from the server
$response = curl_exec( $ch );

curl_close($ch);        

Here you can continue to use GET requests on the same page now with this cookie with the use of:

curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookieJar); // other stuff is the same as before
trainoasis
  • 6,419
  • 12
  • 51
  • 82