cancel
Showing results for 
Search instead for 
Did you mean: 

I Have to post data to Odata webservice. In this post I need to pass token header also. I got token value now I have to try to pass Token Header value and Data also but when i do this I'm getting this kind of ERROR: Stream was not readable

former_member198924
Participant
0 Kudos

  try

            {

           

                byte[] auth = Encoding.UTF8.GetBytes("mobtest" + ":" + "welcome1");

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://Server:8000/sap/opu/odata/sap/ZCUST_TESTING_SRV/post_cust");

                //byte[] byteArray = Encoding.UTF8.GetBytes(postXml);

                byte[] byteArray = Encoding.ASCII.GetBytes(jsonnew);

                request.Credentials = new NetworkCredential("mobtest","welcome1");

                request.ContentType = "application/atom+xml";

                request.Method = "GET";

                //request.Headers["Authorization"] = "Basic " + System.Convert.ToBase64String(auth);

                request.Headers["X-CSRF-Token"] = "Fetch";

                HttpWebResponse responseH = (HttpWebResponse)request.GetResponse();

                xToken = responseH.Headers["X-CSRF-Token"];

                Console.WriteLine("xToken : " + xToken.ToString());

                responseH.Close();

                try

                {

                    HttpWebRequest request_post = (HttpWebRequest)WebRequest.Create("http://Server:8000/sap/opu/odata/sap/ZCUST_TESTING_SRV/post_cust");

                    request_post.Credentials = new NetworkCredential("mobtest", "welcome1");

                    request_post.Method = "POST";

                    request_post.Headers["X-CSRF-Token"] = xToken;

                   

                    Console.WriteLine("xToken POST : " + xToken.ToString());

                    request_post.ContentType = "application/json";

                    request_post.Accept = "application/json";

                    request_post.ContentLength = byteArray.Length;

               

                    Stream dataStream1 = request_post.GetRequestStream();

                    dataStream1.Write(byteArray, 0, byteArray.Length);

                    Console.WriteLine("byteArray : "+byteArray);

                    dataStream1.Close();

                    Console.WriteLine("datastream1");

                    HttpWebResponse response1 = (HttpWebResponse)request.GetResponse();

                    Console.WriteLine("Post response : " + response1.ToString());

                    dataStream1 = response1.GetResponseStream();

                  

                    StreamReader reader1 = new StreamReader(dataStream1);

                    Console.WriteLine("  Console.WriteLine(reader1 : " + reader1);

                    string responseFromServer1 = reader1.ReadToEnd();

                    Console.WriteLine("  Console.WriteLine(responseFromServer1 : " + responseFromServer1);

                    Console.Read();

                    reader1.Close();

                    dataStream1.Close();

                    response1.Close();

                 

                }

                catch(Exception exp)

                {

                    MessageBox.Show("Post Error : "+exp.Message);

                }

            }

            catch(Exception ex)

            {

                MessageBox.Show("Error : "+ex.Message);

            }

Accepted Solutions (0)

Answers (1)

Answers (1)

AshwinDutt
Active Contributor
0 Kudos

Hello Venkat,

Looks like the error is because of wrong payload ( data ) format.

Cross check the payload format u r sending from ur application to GW.

Can u share the payload which is build from ur UI5 application ? I mean to say share the payload created in ur application before u fire the GW post service.

Regards,

Ashwin

former_member198924
Participant
0 Kudos

Hi Ashwin,

                  That is C# windows application below i mention the XML data and Code also.

string postXml = @"<?xml version='1.0' encoding='UTF-8'?>"+

"<entry xml:base='http://serverip:8000/sap/opu/odata/sap/ZCUST_TESTING_SRV/'\n"+"xmlns='http://www.w3.org/2005/Atom'"+

"xmlns:m='http://schemas.microsoft.com/ado/2007/08/dataservices/metadata'"+

"xmlns:d='http://schemas.microsoft.com/ado/2007/08/dataservices'>"+

"<content type='application/xml'>"+

"<m:properties>"+

"<d:Kunnr>800DUR298</d:Kunnr>" +

"<d:Land1>avi</d:Land1>" +

"<d:Name1>dur</d:Name1>" +

"<d:Name2>ven</d:Name2>" +

"<d:Ort01>chennai</d:Ort01>" +

"<d:Pstlz>500060</d:Pstlz>" +

"<d:EStatus>X</d:EStatus>"+

"</m:properties>"+

" </content>"+

" </entry>";

  try

            {

          

                byte[] auth = Encoding.UTF8.GetBytes("mobtest" + ":" + "welcome1");

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://Server:8000/sap/opu/odata/sap/ZCUST_TESTING_SRV/post_cust");

                byte[] byteArray = Encoding.UTF8.GetBytes(postXml);

      

                request.Credentials = new NetworkCredential("mobtest","welcome1");

                request.ContentType = "application/xml";

                request.Method = "GET";

         

                request.Headers["X-CSRF-Token"] = "Fetch";

                HttpWebResponse responseH = (HttpWebResponse)request.GetResponse();

                xToken = responseH.Headers["X-CSRF-Token"];

                Console.WriteLine("xToken : " + xToken.ToString());

                responseH.Close();

                try

                {

                    HttpWebRequest request_post = (HttpWebRequest)WebRequest.Create("http://Server:8000/sap/opu/odata/sap/ZCUST_TESTING_SRV/post_cust");

                    request_post.Credentials = new NetworkCredential("mobtest", "welcome1");

                    request_post.Method = "POST";

                    request_post.Headers["X-CSRF-Token"] = xToken;

                  

                    Console.WriteLine("xToken POST : " + xToken.ToString());

                    request_post.ContentType = "application/xml";

                    request_post.Accept = "application/xml";

                    request_post.ContentLength = byteArray.Length;

              

                    Stream dataStream1 = request_post.GetRequestStream();

                    dataStream1.Write(byteArray, 0, byteArray.Length);

                    Console.WriteLine("byteArray : "+byteArray);

                    dataStream1.Close();

                    Console.WriteLine("datastream1");

                    HttpWebResponse response1 = (HttpWebResponse)request.GetResponse();

                    Console.WriteLine("Post response : " + response1.ToString());

                    dataStream1 = response1.GetResponseStream();

                 

                    StreamReader reader1 = new StreamReader(dataStream1);

                    Console.WriteLine("  Console.WriteLine(reader1 : " + reader1);

                    string responseFromServer1 = reader1.ReadToEnd();

                    Console.WriteLine("  Console.WriteLine(responseFromServer1 : " + responseFromServer1);

                    Console.Read();

                    reader1.Close();

                    dataStream1.Close();

                    response1.Close();

                

                }

                catch(Exception exp)

                {

                    MessageBox.Show("Post Error : "+exp.Message);

                }

            }

            catch(Exception ex)

            {

                MessageBox.Show("Error : "+ex.Message);

            }

Message was edited by: jitendra kansal

Message was edited by: jitendra kansal

AshwinDutt
Active Contributor
0 Kudos

Hello Vankat,

The below is the sample standard xml payload/body .

Refer to the below and create ur body in that format.

Body ->

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"

  xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"

  xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">

  <atom:content type="application/xml">

  <m:properties>

  <d:IvManuCompany>0100</d:IvManuCompany>

  <d:RequestNo>TEST</d:RequestNo>

  <d:RequestItmno>0000</d:RequestItmno>

  <d:FromPackage>NZ00</d:FromPackage>

  <d:ToPackage>NZ00</d:ToPackage>

  <d:Quantity>1.000</d:Quantity>

  <d:BaseUnit>TON</d:BaseUnit>

  </m:properties>

  </atom:content>

</atom:entry>

Headers ->

Content-Type - application/xml

x-csrf-token - fetch

Cross check whether the body created in the application before u fire POST operation is in the above format or not.

Regards,

Ashwin

former_member198924
Participant
0 Kudos

Hi Ashwin,

                  I tried your xml format that one also not working,I'm getting same Error : OData Post request returning 403 forbidden. please do you send any other solutions.

AshwinDutt
Active Contributor
0 Kudos

Hello Durai,

Did u send the headers which i shared u ? Cross check again. If u correctly send body and appropriate headers you should be able to post.

Regards,

Ashwin

former_member198924
Participant
0 Kudos

yeah , I sent headers also but still I'm getting the same 403 forbidden Error if u have any post request code pls send to me.

AshwinDutt
Active Contributor
0 Kudos

Hello Venkat,

Sorry i cannot help you regarding UI level code part as i do not have knowledge on that.

But with the details what i have shared in previous mail, with that u shud be able to post if u send all those headers and payload in the desired format.

Regards,

Ashwin

AshwinDutt
Active Contributor
0 Kudos

U can debug ur UI code and check what is the data generated ( xml payload ). Also check the URL u r passing. Check the headers as well.

Sample URL  ->

/sap/opu/odata/sap/<Your Service Name>/<Entity Name>

Body ->

<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"

  xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"

  xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">

  <atom:content type="application/xml">

  <m:properties>

  <d:IvManuCompany>0100</d:IvManuCompany>

  <d:RequestNo>TEST</d:RequestNo>

  <d:RequestItmno>0000</d:RequestItmno>

  <d:FromPackage>NZ00</d:FromPackage>

  <d:ToPackage>NZ00</d:ToPackage>

  <d:Quantity>1.000</d:Quantity>

  <d:BaseUnit>TON</d:BaseUnit>

  </m:properties>

  </atom:content>

</atom:entry>

Headers ->

Content-Type - application/xml

x-csrf-token - fetch

Method ->

POST

Regards,

Ashwin