0

I am having problems converting a curl command to c# code.

I have been given a curl command which includes an API key as follows:

curl -k -u x:459c4da6401d39bbf9327ee17175e25c 
        -H "Content-Type: application/json" https://disney.com/v1/services.json

When I watch this call in Fiddler I can see a header value that looks like this:

Authorization: Basic eDo0NTljNGRhNjQwMWQzOWJiZjkzMjdlZTE3MTc1ZTI1Yw==

So, the curl command is working perfectly... But I cannot replicate it in c# code

I don't understand how the x:459c4da6401d39bbf9327ee17175e25c has changed to Authorization: Basic eDo0NTljNGRhNjQwMWQzOWJiZjkzMjdlZTE3MTc1ZTI1Yw==

And subsequently, I am confused how to get my c# code to change from the API key to what I am seeing in Fiddler.

When I use this code to add a header the authorisation fails:

httpWebRequest.Headers.Add("Authorization", "459c4da6401d39bbf9327ee17175e25c");

I have also tried:

httpWebRequest.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("459c4da6401d39bbf9327ee17175e25c"));

But that produces a header value of this:

Authorization: Basic NDU5YzRkYTY0MDFkMzliYmY5MzI3ZWUxNzE3NWUyNWM=

Can anyone help please?

Thanks

EricLaw
  • 56,563
  • 7
  • 151
  • 196
Trevor Daniel
  • 3,785
  • 12
  • 53
  • 89

1 Answers1

1

eDo0NTljNGRhNjQwMWQzOWJiZjkzMjdlZTE3MTc1ZTI1Yw== is the base64 encode of x:459c4da6401d39bbf9327ee17175e25c. Try online here.

And the request header for Authorization will be the base64 string that you encoded above.

httpWebRequest.Headers.Add("Authorization", "Basic eDo0NTljNGRhNjQwMWQzOWJiZjkzMjdlZTE3MTc1ZTI1Yw==");

You can use this link to perform base64 encoding/decoding in c#.

Community
  • 1
  • 1
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85