Translate cUrl to restsharp

2020-05-03 11:08发布

问题:

I have a cUrl request:

curl -v -u admin:geoserver -XPUT -H "Content-type: application/zip" --data-binary @C:\nyc_khr.zip  http://localhost:9090/geoserver/rest/workspaces/nyc_roads/datastores/roads/file.shp

I need make a request like above, but using C#
Example of my code:

RestClient client = new RestClient(URL)
{
      Authenticator = new HttpBasicAuthenticator("admin", "geoserver")
};

var data = File.ReadAllBytes(somePath);


string url = @"rest/workspaces/nyc_roads/datastores/roads/finalas.shx";
var request = new RestRequest(url, Method.PUT);
request.AddHeader("Content-Type", "application/zip");

request.AddFile("finalik.zip", data, "finalik.zip", "application/zip");
var response = client.Execute(request);
var res = response.ContentType;

回答1:

instead of

request.AddFile()

this worked for me:

request.Parameters.Clear();
request.AddParameter("application/zip", data, ParameterType.RequestBody);