andrewb
andrewb

Reputation: 3095

Using RestSharp to submit a POST request, values are added to header

I am trying to submit some data to an API using RestSharp in C# and it seems all of my parameters are added to the Headers collection - or that is just how they are catalogued in VS.

Here is my code

var client = new RestClient("https://api.com");
var request = new RestRequest("/recognize", Method.POST);

request.AddHeader("app_id", "");
request.AddHeader("app_key", "");
request.AddHeader("Content-Type", "multipart/form-data");

request.AddParameter("gallery_name", model.gallery_name);
request.AddParameter("image", model.image);

var response = client.Execute(request);

The error back from the API says the request is missing the gallery_name and image parameters, but looking into the request object they are there in the Headers collection.

I can make the call in Postman where the method is set to Post and the Body is set to form-data, along with 2 key/value pairs listed.

What am I doing wrong?

Upvotes: 0

Views: 3825

Answers (1)

Antoon Verroken
Antoon Verroken

Reputation: 192

In your question you says "I can make the call in Postman ...".

Normally if the Postman request is successful then should the generated RestSharp code from Postman works, if there are no open issues in the RestSharp library that can cause your problem.

It's hard to debug and reproduce the problem from here, but I can give the following that you can check.

Check this:

If your Postman request works and is successful then check the Restsharp code generated by Postman:

  1. Click on "code"

Step 1: Get the generated code from Postman

  1. Choose in the combobox for "C# (RestSharp)"

See the Restsharp code from the postman request

  1. Try the RestSharp code that you see in the window, if the code doesn't work check this URL for known issues.

On a sidenote: If you see a known issue that can cause your error, try to test with a previous version of RestSharp or an alpha version.

I hope you can now further investigate and debug the problem.

(I had similar problems and resolved it successful with this way of investigating and debugging)

Upvotes: 6

Related Questions