0

I've read and re-read stackoverflow and google searches in general and I just can't seem to find a solution to my issue. I'm positive it is my ignorance.

I am trying to reproduce a cUrl call in .Net (c# specifically) and am having a devil of a time wading through and figuring out why it isn't working. this cUrl call uses a token and the username is not required when I do so.

The following cUrl call works as expected when called from the command line. The API token has been changed to protect the innocent.

curl -u x:8cb60a319c71be3356da2ea6d7c7650b -H "Content-Type: application/json" https://deskapi.gotoassist.com/v1/incidents.json

I have tried the following:

           WebRequestHandler webHandler = new WebRequestHandler();
        webHandler.Credentials = new System.Net.NetworkCredential("x", "8cb60a319c71be3356da2ea6d7c7650b");

        HttpClient client = new HttpClient(webHandler);

        client.BaseAddress = new Uri("https://deskapi.gotoassist.com/v1/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage response = await client.GetAsync("incidents.json");

        if (response.IsSuccessStatusCode)
        {
            Console.Write("Success!");
        }

The response gives a "Bad Request". The actual requestmessage is:

If that helps any.

Based on other stackoverflow examples I also tried:

            HttpClient client = new HttpClient();

        client.BaseAddress = new Uri("https://deskapi.gotoassist.com/v1/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("8cb60a319c71be3356da2ea6d7c7650b");

        HttpResponseMessage response = await client.GetAsync("incidents.json");

        if (response.IsSuccessStatusCode)
        {
            Console.Write("Success!");
        }

        Console.Write(response.RequestMessage.ToString());

With another "Bad Request". The message is similar:

  • response.RequestMessage {Method: GET, RequestUri: 'https://deskapi.gotoassist.com/v1/incidents.json', Version: 1.1, Content: , Headers: { Accept: application/json Authorization: 8cb60a319c71be3356da2ea6d7c7650b }} System.Net.Http.HttpRequestMessage

The actual uri looks correct to me. The content type looks correct to me. The only thing I can't find a definitive example to do is send that "x:8cb60a319c71be3356da2ea6d7c7650b" other than the ways I have tried it.

Any thoughts? This is my first time trying to do anything like this so hopefully I'm just doing some ignorant something-something. I've tried to follow example after example but nothing gets me an successful call. Thank you in advance for helping.

2 Answers 2

1

The cUrl command uses the Content-Type header which is only intended for when you are sending a body e.g. with PUT or POST. It is not the same header as Accept. Accept is the correct header to send with a GET request.

When I try hitting the server without any auth header, I get back a 400 with the message "Unknown OAuth signature method". The response for missing auth should really be a 401 and a www-authenticate header that tells us what kind of scheme to use.

Assuming that the server is actually looking for a OAuth2 bearer token, then you might want to try,

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer","8cb60a319c71be3356da2ea6d7c7650b");
Sign up to request clarification or add additional context in comments.

6 Comments

Are you suggesting I remove the line that sends the header of "application/json" and replace it with something else? Assuming you mean to remove that line I did... and added the "bearer" to the auth. I also received the "[E901] Unknown OAuth signature method" like you did.
@MichaelHenry I can't really tell you whether you need the Accept header or not. All I know is that sending Content-Type with the GET is not correct as far as HTTP goes. I guess the service you are accessing doesn't care. Unfortunately you are going to need to find more documentation from the service provider.
@MichaelHenry From looking at the docs for your service it appears that they stupidly insist on you sending that header. In HttpClient the ContentType is set on the HttpContent headers which you don't have because you are not sending content. You can try adding the the header manually using request.Headers.AddWithoutValidation, but I suspect it is going to stop you. You may have to resort back to HttpWebRequest.
Thank you for all the suggestions and help. When I get back to work tomorrow I'll beat on this a bit more. In the end this is just the "first step". Once I get this licked and maybe one more hurdle a few cool things will happen.
Small update. The Curl command DOES work without the content type being set, as you suggested. Now off to try the other things.
|
0

Debug your code using Fiddler

By using Fiddler you will exactly know what is happenning during these calls,

  • Run it against CURL command line
  • Run it against your code

Basically you have nothing to do, just install it, run it and do some Internet activity. You will then see what's being intercepted.

To fix your problem, look at the Inspectors/Raw tab and compare them.

Here's an example of an issue I've had and what I found out that was missing with Fiddler :

Equivalent of "curl -F" parameter for System.Net.Http.MultipartFormDataContent?

6 Comments

Thanks for the thoughts on fiddler. I did install fiddler but it is not capturing my command line version. It DID capture my .net app when it ran. I'll have to see if there is anything I have to do in order to capture the command line run.
@MichaelHenry If you can't convince cUrl to send your request through fiddler, you could use www.Runscope.io as a proxy to look at the request it sends.
Thanks. I'll give that a go when I get back to work tomorrow.
@MichaelHenry I just tested here and it works fine with a CLI app. Try to start Fiddler before VS or kill the VS host executable in Task Manager or disable VS hosting process in Properties -> Debug.
@Aybe I tried that as well as restarting my system and doing the Fiddler and cUrl and still no tracking.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.