jre247
jre247

Reputation: 1887

Curl request written in C# doesn't work

I'm trying to write a specific curl request in C#, and I keep getting a 500 server error response from the server. This curl request essentially makes a post request to an API by the company Highwinds. This request sends json data, and sets the Auth Bearer token header.

This is the curl request that works fine (note that I've replaced my actual bearer token with {token} and my actual account id with {accountId} to obfuscate that info):

curl -H "Authorization: Bearer {token}" -H "Content-Type: application/json" -d "@data.json" "https://striketracker.highwinds.com/api/accounts/{accountId}/purge"

Here's the C# code that gives me a generic 500 server error from the Highwinds API (note that I've replaced my actual bearer token with {token}, my actual account id with {accountId}, and the url in the json string with {url}, in order to obfuscate that personal info):

var accountId = "{accountId}";
var purgeURI =  string.Format("https://striketracker.highwinds.com/api/accounts/{0}/purge", {accountId});
var query =
@"{""list"": [{""url"": ""{url}"",""recursive"": true}]}";
var token = {token};


using (var httpClient = new HttpClient())
{

var url = new Uri(purgeURI);
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, url))
{
    httpRequestMessage.Headers.Add(System.Net.HttpRequestHeader.Authorization.ToString(),
      string.Format("Bearer {0}", token));

    httpRequestMessage.Content = new StringContent(query,
                Encoding.UTF8,
                "application/json");

    await httpClient.SendAsync(httpRequestMessage).ContinueWith(task =>
    {

        var response = task.Result;
        var blah = response.Content.ReadAsStringAsync().Result;

        Console.WriteLine(response.Content.ReadAsStringAsync().Result);
    });
}
}

Thanks!

*Update: The following line of code was added to remove the Expect header that HttpRequest adds to a request by default. After removing this header I was able to get Highwinds API to accept the request without bombing.

"request.ServicePoint.Expect100Continue = false;"

Upvotes: 2

Views: 1283

Answers (1)

Brian Riley
Brian Riley

Reputation: 946

My best recommendation would be to proxy both requests through something like tcpmon http://archive.apache.org/dist/ws/tcpmon/1.0/ (Basically run the server and point to local host and have tcpmon redirect the request to striketracker.highwinds.com). Try it from curl and from your source and you should be able to see what's different between the requests.

Upvotes: 2

Related Questions