Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET MVC JSON webapi
In my scenario my API handles the json reading it as
 
if (context.Request.Files.Count > 0)
{
jsonData = context.Request.Params["key"];
}
 
from my application how can i send a web request to this api without json as query string parameter. Sine Json is lengthy and i know query string is limited.
 
For android an ios this api works fine.
 
i have tried to add it to the header. but in vain.
 
how can i add it so that "jsonData = context.Request.Params["key"]; " will get my json.
my request format is here.
                urlS="http://abc.ashx?Key="+jsonRequestS;
                string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
                var webRequest1 = (HttpWebRequest)WebRequest.Create(urlS);
                webRequest1.Method = "POST";
                webRequest1.KeepAlive = false;
                webRequest1.ContentType =string.Format("multipart/form-data; boundary={0}", boundary);//                
                Stream postDataStream1 = handler.GetPostStream(boundary,jsonRequestS);               // Writes boundary and files to memory stream.

                webRequest1.ContentLength = postDataStream1.Length;
                Stream reqStream1 = webRequest1.GetRequestStream();
 
                postDataStream1.Position = 0;
 
                var bufferBytes = new byte[postDataStream1.Length];
                postDataStream1.Read(bufferBytes, 0, bufferBytes.Length);
                reqStream1.Write(bufferBytes, 0, bufferBytes.Length);
 
                postDataStream1.Close();
                reqStream1.Close();
 
                var sReader = new StreamReader(webRequest1.GetResponse().GetResponseStream());//here Am getting the error.
                string resultS = sReader.ReadToEnd();
 
Thanks in advance.
Posted 6-Mar-13 22:05pm
Edited 7-Mar-13 18:20pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello Ashish,
 
Looking at your client code snippet first thing you need to do is to change very first line to read as follows
urlS="http://abc.ashx";
The next thing is to change the line which creates postDataStream1. On this line you are passing boundry & jsonRequestS to GetPostStream. Assuming that jsonRequestS is a string all you need to do is change that line to
handler.GetPostStream(boundary, "key=" + jsonRequestS);

Remember POST data is sent in "key=value" format. Since you are transferring any file(s), ContentTypeas "multipart/form-data; boundary=-------------------------334234ds" is unnecessary. ContentType as "application/x-www-form-urlencoded" should be sufficient.
 
regards,
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Thanks for ur Reply. I found a way to solve this.
 
Write the Json to the request stream with the header as
 
postDataStream.Write(boundarybytes, 0, boundarybytes.Length);
string header = string.Format("Content-Disposition: form-data; name=\"Key\"\r\n\r\n");
headerbytes = Encoding.Default.GetBytes(header);
postDataStream.Write(headerbytes, 0, headerbytes.Length);
headerbytes = Encoding.Default.GetBytes(jsonRequestS);//writing json request.
postDataStream.Write(headerbytes, 0, headerbytes.Length);
and at the server side it will be fine read in the statement
 
jsonData = context.Request.Params["key"];
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 8 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100