locked
Getting NULL Deserialize value using Web API Passing Token and ID RRS feed

  • Question

  • User-890662221 posted

    I am new to web api. I am not sure if my syntax is correct. Please if you can provide some guidance.

    the web api takes two "parameters". #1 A token (which I retrieved from another api) and #2 A ID from another table.

    I am calling a web api passing in a token in the header and a json id. But I am getting Null from JsonConvert.DeserializeObject<string>(myHttpWebResponse.Headers["ReqId"]);

    private string HttpPostRequest(string url, string token)
            
    
              
                GetID md = new GetID ();
                md.IdStr = Id;
    
                postData = JsonConvert.SerializeObject(md);
    
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                myHttpWebRequest.Method = "POST";
    
                byte[] data = Encoding.ASCII.GetBytes(postData);
    
                myHttpWebRequest.ContentType = "application/json; charset=utf-8";
                myHttpWebRequest.ContentLength = data.Length;
                myHttpWebRequest.Headers.Add("Authorization", "Bearer " + token);
                myHttpWebRequest.Timeout = 1200000; // 20 min
    
                Stream requestStream = myHttpWebRequest.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
    
                HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
                {
    
                    string reqID = JsonConvert.DeserializeObject<string>(myHttpWebResponse.Headers["ReqId"]);
    
                    Stream responseStream = myHttpWebResponse.GetResponseStream();
    
                    StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default);
    
                     pageContent = myStreamReader.ReadToEnd();
    
                    myStreamReader.Close();
                    responseStream.Close();
    
                    myHttpWebResponse.Close();
     }
                    return pageContent;
               
            }
    Monday, November 2, 2015 7:33 AM

All replies

  • User-890662221 posted

    Got it working. I was requesting the wrong Header.

    Monday, November 2, 2015 2:41 PM
  • User-219423983 posted

    Hi abdamian,

    I’m glad to hear that you have solve your issue, thank you for your posting.

    Best Regard,

    Weibo Zhang

    Monday, November 2, 2015 10:08 PM