1 year ago
#354919
Steve Wilford
How to post a URL encoded dictionary within a strongly typed object from Refit
I am trying to replicate a URL Encoded Form Data request that contains a dictionary from Postman into Refit but I cannot get it (Refit) to produce the same encoded data. See the comparisons below and notice that the encoded data from Refit has seemingly done .ToString()
on the dictionary rather than encoding it the same way that Postman has.
Is this functionality built in to Refit or will I somehow have to implement a custom encoder? It seems like this would be a fairly common use-case so I'm not sure what I'm missing.
Postman
Request and Url Encoded form data:
Body=Test&AllowContact=true&Context=postman&QuestionsAnswers%5B0%5D.Key=Question&QuestionsAnswers%5B0%5D.Value=2
Refit
API definition:
[Post("/feedback")]
Task<ApiResponse<HttpResponseMessage>> PostFeedback(
[Body(BodySerializationMethod.UrlEncoded)] PostFeedbackRequest request);
Request definition:
public record PostFeedbackRequest(
string Body,
bool AllowContact,
string Context,
IDictionary<string, int>? QuestionsAnswers = null);
API call:
var response = await Api.PostFeedback(
new PostFeedbackRequest(
"Test",
true,
"Refit",
new Dictionary<string, int>
{
["Question"] = 2,
}));
Url Encoded form data:
Body=Test&AllowContact=True&Context=Refit&QuestionsAnswers=System.Collections.Generic.Dictionary%602%5BSystem.String%2CSystem.Int32%5D
c#
.net-core
postman
refit
0 Answers
Your Answer