Skip to content

Commit 11924e1

Browse files
committed
add clearer AddJsonBody & AddXmlBody methods
It can be unintuitive that you need to first specify the RequestFormat is JSON to have the RestRequest serialize data to JSON. This leads to hard to debug HTTP responses saying that the JSON format is incorrect, because by default the data is serialized to XML. This is further compounded by endless suggestions on popular forums and message boards that you manually need to set the ‘Content-Type’ header to be ‘application/json’ making this a difficult product to use out of the box.
1 parent bb02736 commit 11924e1

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

RestSharp/IRestRequest.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ public interface IRestRequest
160160
#endif
161161

162162
/// <summary>
163-
/// Serializes obj to format specified by RequestFormat, but passes xmlNamespace if using the default XmlSerializer
163+
/// Serializes obj to format specified by RequestFormat, but passes xmlNamespace if using the default XmlSerializer.
164+
/// The default format is XML. Change RequestFormat if you wish to use a different serialization format.
164165
/// </summary>
165166
/// <param name="obj">The object to serialize</param>
166167
/// <param name="xmlNamespace">The XML namespace to use when serializing</param>
@@ -169,11 +170,36 @@ public interface IRestRequest
169170

170171
/// <summary>
171172
/// Serializes obj to data format specified by RequestFormat and adds it to the request body.
173+
/// The default format is XML. Change RequestFormat if you wish to use a different serialization format.
172174
/// </summary>
173175
/// <param name="obj">The object to serialize</param>
174176
/// <returns>This request</returns>
175177
IRestRequest AddBody (object obj);
176178

179+
/// <summary>
180+
/// Serializes obj to JSON format and adds it to the request body.
181+
/// </summary>
182+
/// <param name="obj">The object to serialize</param>
183+
/// <returns>This request</returns>
184+
IRestRequest AddJsonBody(object obj);
185+
186+
/// <summary>
187+
/// Serializes obj to XML format and adds it to the request body.
188+
/// </summary>
189+
/// <param name="obj">The object to serialize</param>
190+
/// <returns>This request</returns>
191+
IRestRequest AddXmlBody(object obj);
192+
193+
/// <summary>
194+
/// Serializes obj to format specified by RequestFormat, but passes xmlNamespace if using the default XmlSerializer
195+
/// Serializes obj to XML format and passes xmlNamespace then adds it to the request body.
196+
/// </summary>
197+
/// <param name="obj">The object to serialize</param>
198+
/// <param name="xmlNamespace">The XML namespace to use when serializing</param>
199+
/// <returns>This request</returns>
200+
IRestRequest AddXmlBody(object obj, string xmlNamespace);
201+
202+
177203
/// <summary>
178204
/// Calls AddParameter() for all public, readable properties specified in the white list
179205
/// </summary>

RestSharp/RestRequest.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ private IRestRequest AddFile (FileParameter file)
206206
}
207207

208208
/// <summary>
209-
/// Serializes obj to format specified by RequestFormat, but passes xmlNamespace if using the default XmlSerializer
209+
/// Serializes obj to format specified by RequestFormat, but passes xmlNamespace if using the default XmlSerializer.
210+
/// The default format is XML. Change RequestFormat if you wish to use a different serialization format.
210211
/// </summary>
211212
/// <param name="obj">The object to serialize</param>
212213
/// <param name="xmlNamespace">The XML namespace to use when serializing</param>
@@ -243,6 +244,7 @@ public IRestRequest AddBody (object obj, string xmlNamespace)
243244

244245
/// <summary>
245246
/// Serializes obj to data format specified by RequestFormat and adds it to the request body.
247+
/// The default format is XML. Change RequestFormat if you wish to use a different serialization format.
246248
/// </summary>
247249
/// <param name="obj">The object to serialize</param>
248250
/// <returns>This request</returns>
@@ -251,6 +253,41 @@ public IRestRequest AddBody (object obj)
251253
return AddBody(obj, "");
252254
}
253255

256+
/// <summary>
257+
/// Serializes obj to JSON format and adds it to the request body.
258+
/// </summary>
259+
/// <param name="obj">The object to serialize</param>
260+
/// <returns>This request</returns>
261+
public IRestRequest AddJsonBody(object obj)
262+
{
263+
RequestFormat = DataFormat.Json;
264+
return AddBody(obj, "");
265+
}
266+
267+
/// <summary>
268+
/// Serializes obj to XML format and adds it to the request body.
269+
/// </summary>
270+
/// <param name="obj">The object to serialize</param>
271+
/// <returns>This request</returns>
272+
public IRestRequest AddXmlBody(object obj)
273+
{
274+
RequestFormat = DataFormat.Xml;
275+
return AddBody(obj, "");
276+
}
277+
278+
/// <summary>
279+
/// Serializes obj to format specified by RequestFormat, but passes xmlNamespace if using the default XmlSerializer
280+
/// Serializes obj to XML format and passes xmlNamespace then adds it to the request body.
281+
/// </summary>
282+
/// <param name="obj">The object to serialize</param>
283+
/// <param name="xmlNamespace">The XML namespace to use when serializing</param>
284+
/// <returns>This request</returns>
285+
public IRestRequest AddXmlBody(object obj, string xmlNamespace)
286+
{
287+
RequestFormat = DataFormat.Xml;
288+
return AddBody(obj, xmlNamespace);
289+
}
290+
254291
/// <summary>
255292
/// Calls AddParameter() for all public, readable properties specified in the white list
256293
/// </summary>

0 commit comments

Comments
 (0)