Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture a response token sent by a REST API after a request?

I am working on consuming a REST API and I am using basic authentication where password is encoded to Base64 as follows

 private XmlDocument sendXMLRequest(string requestXml)
    {
        string destinationUrl = "https://serviceapi.testgroup.com/testtp/query";

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
        request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("API_TEST_NR:Testnol1$"));
        byte[] bytes;
        bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
        request.Method = "POST";

        request.ContentLength = bytes.Length;
        //request.Connection = "keep-alive";
        request.ContentType = "text/xml";
        request.KeepAlive = true;
        request.Timeout = 2000;
        request.MediaType = "text/xml";
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(bytes, 0, bytes.Length);
        requestStream.Close();
        HttpWebResponse response;
        Stream responseStream;

        using (response = (HttpWebResponse)request.GetResponse())
        {
            if (response.StatusCode == HttpStatusCode.OK)
            {
                responseStream = response.GetResponseStream();
                XmlReader reader = new XmlTextReader(responseStream);

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(reader);


                try { reader.Close(); }
                catch { }
                try { responseStream.Close(); }
                catch { }
                try { response.Close(); }
                catch { }
                return xmlDoc;
            }
        }
        try { response.Close(); }
        catch { }
        return null;
    } 

I'm kind of new to working on Web Api's and I know that the API responds with an access x-token after successful authorization based on the API documentaion and I am not sure how to access or capture it from the HTTP headers.

May I know a good way I can achieve this?

like image 806
DoIt Avatar asked Jul 01 '15 15:07

DoIt


People also ask

How can I get token from POST request?

To get the API token for a user, an HTTP POST request should be sent to the Token resource. In the post body, username and password are specified in JSON format, and the response body contains a token key with an actual API Token as the value.

How do I get a response token?

Convert the response string into JSONObject and then again get the JSONObject for data and in data you can find your token object.

HOW CAN I GET REST API token?

You use the POST operation on the api/get_token element to request your unique token that is required to authenticate the REST API requests. , and click Profile. Then, click Show token.


1 Answers

This is easier than I thought just capturing with its name.

string xtoken= response.Headers["custom-header"];
Console.WriteLine(xtoken);
like image 170
DoIt Avatar answered Oct 02 '22 18:10

DoIt