PUT/POST action with JSON in Request Body.

2,536 views
Skip to first unread message

cbj72

unread,
May 26, 2011, 10:56:55 AM5/26/11
to RestSharp, PWill...@kaplan.edu
We are currently trying to implement this framework into our
architecture. Everything works fine so far, except for our PUT/POST
calls. Our original method involved putting a JSON string in the
content of the request, with a contenttype of "application/x-www-form-
urlencoded". Here is our original code:

myRequest.Method = action;
myRequest.ContentLength = dataPackage.Length;
myRequest.ContentType = "application/x-www-form-
urlencoded";
if (action.ToLower().Contains("post") ||
action.ToLower().Contains("put") ||
action.ToLower().Contains("delete"))
{
StreamWriter myWriter = new
StreamWriter(myRequest.GetRequestStream());
myWriter.Write(dataPackage);
myWriter.Close();
}

We have attempted several different scenarios to replicate this with
the rest sharp framework with no luck. Here are the methods we've
tried.

var client = new RestClient(info.Url);
var restRequest = new RestRequest(methodAction);
restRequest.RequestFormat = DataFormat.Json;
restRequest.AddHeader(header.Keys[0],
header[header.Keys[0]]);
foreach (string key in info.Parameters.AllKeys)
{
restRequest.AddParameter(key,
info.Parameters[key]);
}
if (methodAction == Method.POST || methodAction ==
Method.PUT || methodAction == Method.DELETE)
{
//Attempted variations
restRequest.AddParameter("application/x-www-form-
urlencoded", data, ParameterType.RequestBody);
restRequest.AddParameter("text/json", data,
ParameterType.RequestBody);
restRequest.AddParameter("text/x-json", data,
ParameterType.RequestBody);
restRequest.AddParameter("application/json", data,
ParameterType.RequestBody);
restRequest.AddParameter("text/plain", data,
ParameterType.RequestBody);
restRequest.AddBody(myObject); //Object that does
get successfully serailized to json
restRequest.AddBody(myJSONString); //string
containing already formatted json
}
var response = client.Execute(restRequest);


For each request, we get 'An error occurred while attempting to parse
the data submitted.'
How can we verify that this is actually getting embedded in the
Content field of the request? (I see it in the parameter collection)
Our framwork allows us to switch between RESTSharp and standard
HTTPWebRequest and we are able to run the original without issue using
the same objects. Any insight would be greatly appreciated as this is
the last hurdle before we can officially integrate the framework.
Thanks!

John Sheehan

unread,
May 26, 2011, 4:49:57 PM5/26/11
to rest...@googlegroups.com
Can you post what the raw request body is supposed to look like? If
you have a link to the specific API docs, that would be sufficient.

John

cbj72

unread,
May 27, 2011, 11:37:09 AM5/27/11
to RestSharp
Here is one example we are working with, a basic JSON string
{"readStatus": {"markedAsRead": false}}
By the time it comes for us to send the request, we've already
serialized the object to a json string.

When I add this object instead of the JSON string, It does appear to
serialize correctly when viewed in the parameter collection:
{application/json={ "readStatus": { "markedAsRead": false }}}

Any suggestions on how do get you the RAW resquest? Unfortunately the
REST service is behind HTTPS.

Here is the API:

Response Read Status
/users/{userId}/courses/{courseId}/threadeddiscussions/{threadId}/
topics/{topicId}/responses/{responseId}/readStatus.{format}

/users/{userId}/courses/{courseId}/threadeddiscussions/{threadId}/
topics/{topicId}/responses/{responseId}/readStatus

/me/courses/{courseId}/threadeddiscussions/{threadId}/topics/{topicId}/
responses/{responseId}/readStatus.{format}

/me/courses/{courseId}/threadeddiscussions/{threadId}/topics/{topicId}/
responses/{responseId}/readStatusGet / Set the read status of a
specific response for a given user
{userId} - A numeric value that represents the ID of the user to get /
set the response read status for
{courseId} - A numeric value that represents the ID of the course of
which the threaded discussion exists within
{threadId} - A numeric value that represents the ID of the threaded
discussion of which the topic exists within
{topicId} - A numeric value that represents the ID of the topic of
which the response exists within
{responseId} - A numeric value that represents the ID of the response
to get / set the read status for
{format} - OPTIONAL. The format of the response. Valid values are
"json" or "xml". Defaults to "json" if the format is not specified.
Supported HTTP Verbs: GET, PUT*

*Note: In order to support PUT methods on older browsers, POST is also
supported as long as one of the following headers is specified on the
request:

X-HTTP-Method: PUTor
X-HTTP-Method-Override: PUT
Example Transactions

Request:

GET http://m-api.ecollege.com/courses/123456/threadeddiscussions/5241/topics/153/responses/204/readStatusRequest
Body: N/A

Response:

HTTP/1.1 200 OKResponse Body:

{
"readStatus":{
"markedAsRead":false
}
}Request:

PUT http://m-api.ecollege.com/courses/123456/threadeddiscussions/5241/topics/153/responses/204/readStatusRequest
Body:

{"readStatus": {"markedAsRead": true}}Response:

HTTP/1.1 204 No ContentResponse Body: N/A


Reply all
Reply to author
Forward
0 new messages